简体   繁体   English

在php mustache中使用键消耗复杂数组

[英]Consuming complex array with keys in php mustache

I have following structure of array arr 我有数组arr以下结构

Array
(
    [1] => Array
        (
            [width] => 600 
            [pages] => Array
                (
                    [0] => Array
                        (
                            [bgColor] => 'red' 
                        )



                ) 
        )

    [3] => Array
        (
            [width] => 400 
            [pages] => Array
                (
                    [0] => Array
                        (
                            [bgColor] => 'blue' 
                        )



                ) 
        )

)

Currently I am passing data as, 目前,我正在传递数据,

$tpl->render(array( 
   'arr'   => new ArrayIterator($arr)                       
));

In in mustache template, I am consuming it like, 在小胡子模板中,我正在消费它,

{{#arr}}  
  {{width}}
{{/arr}}

It gives me width correctly. 它给我正确的width But now I want the keys of that array ( 1 for first and 3 for second one) and also the total no of elements in pages key. 但现在我想的keys该数组的( 1为第一和3为第二个),并且还在总没有元素的pages键。

How can I do this mustache ? 我该怎么留胡子?

Okay, I understand that mustache cannot keep track of index of array and it needs everything in hash . 好的,我知道小胡子不能跟踪数组的index ,它需要hash所有内容。

So, I am using following technique, it works but little bit ugly. 因此,我正在使用以下技术,但效果有点差。

function prepareForMustache ($arr) {
    foreach($arr as $k => &$v) {
        $v['key']  = $k;
        $v['pagesCount'] = count($v['pages']);
    } 
}


$arr = prepareForMustache($arr);

$tpl->render(array( 
   'arr'   => new ArrayIterator($arr)                       
));

And consuming in mustache template as, 并在胡子模板中消费为

{{#arr}}  
  {{width}}
  {{key}}
  {{pagesCount}}
{{/arr}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM