Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mustache Template for nested array

Tags:

php

mustache

How can I loop through this using mustache? I have an outer array of numeric indexes. Or how to do something else and keep this structure?

I tried:

<ul>
{{#cars}}  
<li> {{#.}} {{.}} {{/.}}</li>
{{/cars}}
</ul>

Array

>   Array(
>             [cars] => Array 
>                 (
>                  [0] => Array
>                      (
>                        [0] => Array
>                           (
>                             [id] => 343443
>                             [name] => Mazda
>                           )
>         
>                         [1] =>
>                           (
>                             [id] => 45353
>                             [name] => Toyota
>                           )
>                      )
>                   [1] => Array 
>                       (
>                          [0] => Array 
>                           (
>                             [id] => 922424
>                             [name] => Camry
>                           )
>                        )
>                   )//end cars
>         )
like image 202
Undermine2k Avatar asked Sep 15 '26 08:09

Undermine2k


1 Answers

Try this template:

<ul>
    {{#cars}}
        {{#.}}
        <li>{{id}} - {{name}}</li>
        {{/.}}      
    {{/cars}}
</ul>

Or convert the array:

array_walk( $array['cars'], function($i) use (&$cars) {
    foreach( $i as $v ) $cars['cars'][] = $v;       
});

, and use this simpler template:

<ul>
    {{#cars}}
        <li>{{id}} - {{name}}</li>  
    {{/cars}}
</ul>
like image 90
Danijel Avatar answered Sep 17 '26 21:09

Danijel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!