【问题标题】:Mustache: read variables from parent section in child section小胡子:从子节中的父节读取变量
【发布时间】:2010-11-01 06:48:17
【问题描述】:

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'
                        ),
                    ),
                ));

【问题讨论】:

    标签: php mustache


    【解决方案1】:

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

    点符号在这种情况下没有帮助——它不会神奇地让您访问父数组。 (顺便说一下,并不是所有的 mustache 解析器都支持点表示法,因此如果您将来有可能将模板与另一种编程语言重用,最好避免使用它。)

    【讨论】:

    • 实际上,我会接受@sofia 的建议并使用 Handlebar.js。它与 mustache 兼容,但添加了一些新功能,如在父上下文中搜索。我遇到了同样的问题,Handlebar 帮我解决了这个问题,而无需更改我的 json 内容。
    • @GustavoCardoso 这个问题正在寻找 PHP 中的答案。
    • 我已经在 in js 中尝试过,似乎 Mustache 允许我通过引用父对象的属性来引用它,如果没有重叠的名称在 children 数组中(如果有,Mustache 会从孩子那里拿起那个)。不过,我还没有测试过 PHP 中发生了什么。
    • 这个答案不正确。 Mustache 确实允许您引用父对象。见Rub's answer
    【解决方案2】:

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

    {{../order_store.id}}
    

    【讨论】:

      【解决方案3】:

      对于某些 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}}
      

      【讨论】:

        【解决方案4】:

        快速回答:是和否。

        • 不,你不能上去 (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}}
        

        【讨论】:

          【解决方案5】:

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

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

          如你所见,我可以在 "if" 语句中使用 picture.id picture.src

          【讨论】:

            【解决方案6】:

            对于 java mustache,您可以在子范围内仅命名父范围内的字段,而无需任何斜线或点,如果子范围内没有此类字段,它将尝试在父范围内找到它。

            数据结构:

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

            模板:

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2013-12-19
              • 2015-03-30
              • 1970-01-01
              • 2021-07-16
              • 1970-01-01
              相关资源
              最近更新 更多