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

时间:2010-11-01 06:48:17

标签: 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}}

示例数据(在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'
                        ),
                    ),
                ));

7 个答案:

答案 0 :(得分: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'
        ),
    ),
));

然后您可以使用标记{{order_store_id}}

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

答案 1 :(得分:7)

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

{{../order_store.id}}

答案 2 :(得分:2)

对于某些Mustache解析器,需要{{order_store.id}}

一个经典的例子是SwaggerCodegen。每个变量的“ name” 属性都会覆盖模型的“ name” 属性。在这种情况下,{{../model.name}}会导致错误。我通过以下方法解决了这个问题:

{{name}} // model (parent) name value
{{#vars}}
    {{model.name}} // model (parent) name value
    {{name}}       // vars (child) name valu
{{/vars}}

答案 3 :(得分:1)

我遇到了同样的问题,空对象不是空的

<div class="photo">
    {{#picture.id}}
        <img src="{{picture.src}}" alt="{{picture.name}}" />
    {{/picture.id}}
</div>

你可以看到我可以在“if”语句中使用picture.id picture.src value

答案 4 :(得分:0)

对于Java胡子,您可以仅在子作用域中的父作用域中命名字段而没有任何斜杠或点,如果子作用域中没有此类字段,它将尝试在父作用域中找到它。

数据结构:

report: {
  footer: 'hello',
  pages: [{
    content: 'blah'    
  }]
}

模板:

{{#pages}}
  <div>{{content}}</div>
  <footer>{{footer}}</footer> <-- that will pull 'footer' field from parent scope
{{/pages}}

答案 5 :(得分:0)

快速回答:是和否。

  • 不,你不能上去(AFAIK)。
  • 是的,您可以访问父元素。但 从顶层,而不是从底层。

你需要用一个你可以引用的父元素来包装它。在这种情况下为“书”。

var data = { 
"book":{
    title: "The big book",
    author: "Book author",
    chapters: [
       { title: "Hat", color: "black", author: "Chapter 1 author" },
       { title: "Cat", color: "red", author: "Chapter 2 author" }
       ]
   } 
}

var template = document.body.innerHTML;
document.body.innerHTML = Mustache.render(template, data);
<script src="http://cdnjs.cloudflare.com/ajax/libs/mustache.js/0.7.2/mustache.min.js"></script>

{{#book}}
<ul>
    
    Here we have a book titled "{{title}}" with different chapters:
    {{#chapters}}<br><br>
    
    <li>
        Title: {{title}} <br/> 
        Color: {{color}} <br/>
        Chapter Author: {{author}} <br/> 
        
        -- Book Author: {{book.author}} -- <br/> 
    </li>
    {{/chapters}}
</ul>
<span>Author: {{author}}</span>

{{/book}}

玩转http://jsfiddle.net/pwts7kqx/

<br> Bonus track. Indexed and nested indexing
<br> Title of 1st chapter: {{book.chapters.0.title}}
<br> Title of 2nd chapter: {{book.chapters.1.title}}

答案 6 :(得分:-2)

这里的技巧是在你要添加的模板的开头:

{{%DOT-NOTATION}}