嵌套数组的小胡子模板

时间:2015-03-25 02:00:43

标签: php mustache

如何使用胡子循环?我有一个外部数字索引数组。或者如何做其他事情并保持这种结构?

我试过了:

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

阵列

>   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
>         )

1 个答案:

答案 0 :(得分:4)

试试这个模板:

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

或转换数组:

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

,并使用这个更简单的模板:

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