Welcome Guest, Not a member yet? Register   Sign In
Use deep nested array in view parser
#1

(This post was last modified: 12-28-2022, 09:31 AM by sjender.)

Hi, I have this array:

Code:
[nav:footer] => Array
        (
            [0] => Array
                (
                    [name] => home
                    [link] =>
                    [children] => Array
                        (
                            [0] => Array
                                (
                                    [name] => child1
                                    [link] => link1
                                )

                            [1] => Array
                                (
                                    [name] => child2
                                    [link] => link2
                                )

                        )

                )

            [1] => Array
                (
                    [name] => Contact
                    [link] => contact.php
                    [children] => 

                )

        )


And my view is:

PHP Code:
{{nav:footer}}
<
h1>{{name}}</h1>
<
h2>{{link}}</h2>


{{
children}}
    <h3>{{name}}</h3>
    <h4>{{link}}</h4>
{{/
children}}

{{/
nav:footer}} 


And this results in:

Code:
home
{{children}}
home
{{/children}}
Contact
contact.php
{{children}}
Contact
contact.php
{{/children}}

The children are obviously not parsed.
Is there a way to achieve this (and even deeper) without having to use php?
Reply
#2

PHP Code:
        $data = [
            'nav:footer' => [
                [
                    'name' => 'Tom',
                    'children' => [['name1' => 'Dick']],
                ],
                [
                    'name' => 'Dick',
                    'children' => [['name1' => 'Henry']],
                ],
                [
                    'name' => 'Henry',
                    'children' => [],
                ],
            ],
        ];
        $template "{nav:footer}\n{name} {children}{name1}{/children}{/nav:footer}"
Reply
#3

Yes, there is a way to achieve this without using PHP. You can use a looping construct such as a "for" loop. For example:


Code:
{{nav:footer}}
<h1>{{name}}</h1>
<h2>{{link}}</h2>

{{#for children}}
    <h3>{{name}}</h3>
    <h4>{{link}}</h4>
{{/for}}

{{/nav:footer}}
Reply




Theme © iAndrew 2016 - Forum software by © MyBB