Skip to content

Commit

Permalink
Merge pull request #34 from JoshuaCarter/1.2
Browse files Browse the repository at this point in the history
Adds back child html functionality, add test, and fixes json strings
  • Loading branch information
Joshua Carter committed Nov 17, 2018
2 parents 12fac66 + decd377 commit ad801e1
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/SilbinaryWolf/Components/ComponentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function generateTemplateCode(array $data, $parser)
}
default:
{
// restricted name error
// restricted name error
if ($propName[0] === '_') {
throw new SSTemplateParseException('Invalid special property type: "' . $propName . '", properties that start with a _ are reserved for special functionality. Available special property types are: "_json".', $parser);
break;
Expand All @@ -73,6 +73,9 @@ public function generateTemplateCode(array $data, $parser)
}
}
}

// handle child html
$php .= self::handleChildHTML($data, $properties, $parser);

// final render call for output php
$php .= "\$val .= Injector::inst()->get('SilbinaryWolf\\Components\\ComponentService')->renderComponent('$componentName', \$_props, \$scope);\nunset(\$_props);\n";
Expand Down Expand Up @@ -139,12 +142,35 @@ private static function handlePropertyJSON($propValue, $parser)
if (is_array($value)) {
$value = self::exportNestedDataForTemplates($value);
}
// handle strings
else if (is_string($value)) {
$value = '"' . $value . '"';
}
$buffer .= self::Prop2PHP($name, $value);
}

return $buffer;
}

private static function handleChildHTML($data, $properties, $parser)
{
if (isset($data['Children']['php'])) {
// handle children property collision
if (isset($properties['children'])) {
throw new SSTemplateParseException('Cannot use "children" as a property name and have inner HTML.', $parser);
}

// construct php
$value = $data['Children']['php'];
$php = "\$_props['children'] = '';\n" . $value;
$php = str_replace("\$val .=", "\$_props['children'] .=", $php);
$php .= "\$_props['children'] = DBField::create_field('HTMLText', \$_props['children']);\n";

return $php;
}
return '';
}

private static function Prop2PHP($name, $value)
{
// turns a value into php code
Expand Down
35 changes: 35 additions & 0 deletions tests/ComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,41 @@ public function testJSONDeeplyNested()
$this->assertEqualIgnoringWhitespace($expectedHTML, $resultHTML, 'Unexpected output');
}

/**
* Test iterating multiple root level properties in templates
*/
public function testJSONMultiRoot()
{
$template = <<<SSTemplate
<:JSONRootTest
_json='{
"Title": "Root Test",
"Root1": [
{"Title": "one"},
{"Title": "two"}
],
"Root2": [
{"Title": "three"},
{"Title": "four"}
]
}'
/>
SSTemplate;
$expectedHTML = <<<HTML
<h1>Root Test</h1>
<div>
<h2>one</h2>
<h2>two</h2>
</div>
<div>
<h2>three</h2>
<h2>four</h2>
</div>
HTML;
$resultHTML = SSViewer::fromString($template)->process(null);
$this->assertEqualIgnoringWhitespace($expectedHTML, $resultHTML, 'Unexpected output');
}

/**
* Taken from "framework\tests\view\SSViewerTest.php"
*/
Expand Down
18 changes: 18 additions & 0 deletions tests/templates/components/JSONRootTest.ss
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

<% if $Title %>
<h1>$Title</h1>
<% end_if %>
<% if $Root1 %>
<div>
<% loop $Root1 %>
<h2>$Title</h2>
<% end_loop %>
</div>
<% end_if %>
<% if $Root2 %>
<div>
<% loop $Root2 %>
<h2>$Title</h2>
<% end_loop %>
</div>
<% end_if %>

0 comments on commit ad801e1

Please sign in to comment.