如何在Mustache的循环表示中获取数组的根值?

时间:2016-10-06 11:32:47

标签: html mustache template-engine

使用Mustache模板时,循环表示如下:

数据:

{animals: [
  {name: "cat"}, 
  {name: "dog"}, 
  {name: "pig"}
]}

模板:

{{#animals}}
  <p>{{name}}</p>
{{/animals}}

结果:

<p>cat</p>
<p>dog</p>
<p>pig</p>

但是如果想要使用的值直接写在数组下,我该如何访问它们? 我写的意思是,

数据:

{animals: [
  "cat", 
  "dog", 
  "pig"
]}

结果:

<p>cat</p>
<p>dog</p>
<p>pig</p>

要获得上述结果,我该如何编写模板?

此致

1 个答案:

答案 0 :(得分:2)

在视图中使用{{.}}。这指的是模板中引用的列表中的当前项。当您使用与对象文字相对的字符串数组时,将显示该数组的内容。如果您使用的是上一个对象文字变量,[object][object]将显示在模板视图中。

参考:https://github.com/janl/mustache.js/

<强>对象

animals: [
  "cat", 
  "dog", 
  "pig"
]

查看

{{#animals}}
  <p>{{.}}</p>
{{/animals}}

<强>输出

<p>cat</p>
<p>dog</p>
<p>pig</p>