Mustache:从子节中的父节读取变量

kha*_*ril 14 php mustache

在子节中,Mustache可以从父节读取变量吗?

例如下面我的例子,我希望{{order_store.id}}从它的父$ order_store [(当前子循环的数组索引)] ['id']中读取变量

template.mustache

{{#order_store}}<table>
    <caption>
        Store Name: {{name}}
        Product Ordered: {{products}}
        Product Weights: {{products_weight}}
    </caption>
    <tbody>
        {{#shipping_method}}<tr>
            <td>
                <input type="radio" name="shipping[{{order_store.id}}]" id="shipping-{{id}}" value="{{id}}" /> 
                <label for="shipping-{{id}}">{{name}}</label>
            </td>
            <td>{{description}}</td>
            <td>{{price}}</td>
        </tr>{{/shipping_method}}
    </tbody>
</table>{{/order_store}}
Run Code Online (Sandbox Code Playgroud)

样本数据(PHP);

                $order_store => array(
                array(
                    'id' => 1,
                    'name' => 'Kyriena Cookies',
                    'shipping_method' => array(
                        array(
                            'id' => 1,
                            'name' => 'Poslaju',
                            'description' => 'Poslaju courier'
                        ),
                        array(
                            'id' => 2,
                            'name' => 'SkyNET',
                            'description' => 'Skynet courier'
                        ),
                    ),
                ));
Run Code Online (Sandbox Code Playgroud)

sb.*_*sb. 8

Mustache不允许您引用父对象.您想要在子部分中显示的任何数据都需要包含在子数组中.

例如:

$order_store => array(
array(
    'id' => 1,
    'name' => 'Kyriena Cookies',
    'shipping_method' => array(
        array(
            'id' => 1,
            'name' => 'Poslaju',
            'description' => 'Poslaju courier',
            'order_store_id' => '1'
        ),
        array(
            'id' => 2,
            'name' => 'SkyNET',
            'description' => 'Skynet courier',
            'order_store_id' => '1'
        ),
    ),
));
Run Code Online (Sandbox Code Playgroud)

然后你可以使用标签{{order_store_id}}.

在这种情况下,点符号无济于事 - 它不会神奇地让您访问父数组.(顺便说一句,所有胡子解析器都不支持点符号,所以如果你有可能在未来将模板重用于其他编程语言,最好避免使用它.)


Sof*_*fia 8

如果要在客户端编译模板,另一个选项是使用与Mustache兼容的HandlebarsJS模板,并使用父符号:

{{../order_store.id}}
Run Code Online (Sandbox Code Playgroud)