diff --git a/src/SilbinaryWolf/Components/ComponentService.php b/src/SilbinaryWolf/Components/ComponentService.php index 4ca0a15..304c3bd 100644 --- a/src/SilbinaryWolf/Components/ComponentService.php +++ b/src/SilbinaryWolf/Components/ComponentService.php @@ -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; @@ -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"; @@ -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 diff --git a/tests/ComponentTest.php b/tests/ComponentTest.php index 2f6bb6d..37000ac 100644 --- a/tests/ComponentTest.php +++ b/tests/ComponentTest.php @@ -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; + $expectedHTML = <<Root Test +
+

one

+

two

+
+
+

three

+

four

+
+HTML; + $resultHTML = SSViewer::fromString($template)->process(null); + $this->assertEqualIgnoringWhitespace($expectedHTML, $resultHTML, 'Unexpected output'); + } + /** * Taken from "framework\tests\view\SSViewerTest.php" */ diff --git a/tests/templates/components/JSONRootTest.ss b/tests/templates/components/JSONRootTest.ss new file mode 100644 index 0000000..0e54862 --- /dev/null +++ b/tests/templates/components/JSONRootTest.ss @@ -0,0 +1,18 @@ + +<% if $Title %> +

$Title

+<% end_if %> +<% if $Root1 %> +
+ <% loop $Root1 %> +

$Title

+ <% end_loop %> +
+<% end_if %> +<% if $Root2 %> +
+ <% loop $Root2 %> +

$Title

+ <% end_loop %> +
+<% end_if %> \ No newline at end of file