diff --git a/src/SilbinaryWolf/Components/ComponentService.php b/src/SilbinaryWolf/Components/ComponentService.php index 6a1b5d4..934c700 100644 --- a/src/SilbinaryWolf/Components/ComponentService.php +++ b/src/SilbinaryWolf/Components/ComponentService.php @@ -95,6 +95,7 @@ public function generateTemplateCode(array $res, $parser) } foreach ($jsonData as $propertyName => $value) { if (is_array($value)) { + $value = self::recursivelyConvertFlatArraysToArrayList($value); $value = 'new '.ArrayList::class.'('.var_export($value, true).')'; } $phpCodeValueParts[] = "\$_props['".$propertyName."'][] = ".$value.";"; @@ -244,4 +245,19 @@ public function renderComponent($name, array $props, SSViewer_Scope $scope) // return $result; } + + private static function recursivelyConvertFlatArraysToArrayList(array $array) + { + foreach ($array as $prop => &$value) { + if (is_array($value)) { + $value = self::recursivelyConvertFlatArraysToArrayList($value); + } + unset($value); + } + if (isset($array[0])) { + $array = new ArrayList($array); + //$array = 'new '.ArrayList::class.'('.var_export($array, true).')'; + } + return $array; + } } diff --git a/tests/ComponentTest.php b/tests/ComponentTest.php index 7af0594..d309dee 100644 --- a/tests/ComponentTest.php +++ b/tests/ComponentTest.php @@ -527,6 +527,64 @@ public function testJSONPropertyErrorHandling() } } + public function testJSONDeeplyNested() + { + $template = << +SSTemplate; + $expectedHTML = << +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/JSONNestedTest.ss b/tests/templates/components/JSONNestedTest.ss new file mode 100644 index 0000000..4174a87 --- /dev/null +++ b/tests/templates/components/JSONNestedTest.ss @@ -0,0 +1,14 @@ +<% if $SubMenu %> + <% loop $SubMenu %> +

$Title

+ <% if $Children %> + + <% end_if %> + <% end_loop %> +<% end_if %>