duanmao1319 2014-05-27 14:30
浏览 18
已采纳

与标签的小胡子模板

If I have the following array

array (
    'people' => array(
        [0] => array('name'=>'name1'),
        [1] => array('name'=>'name2', 'last' => true)
    )
);

and I want to output it using a Mustache template so that the final html looks like this:

<div>
    Names: name1, name2
</div>

Though if the array is empty then it shouldn't output anything.

This is my current template

<div>
    {{#people}}
        Names: {{name}}{{^last}}, {{/last}}
    {{/people}}
</div>

which outputs

<div>
    Names: name1, Names: name2
</div>

I understand why it doesn't output what I want but I don't know how to solve it.

  • 写回答

1条回答 默认 最新

  • dtz55359 2014-05-27 14:53
    关注
    <div>
        {{#people.0}}
            Names:
            {{#people}}
                {{name}}{{^last}}, {{/last}}
            {{/people}}
        {{/people.0}}
    </div>
    

    The reason you were getting Names: written twice was because everything inside {{#people}} and {{\people}} is treated as a loop.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!