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

How to get root values of array in Mustache's loop representation?

当使用 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>

要得到上面的结果,我该如何编写模板?

此致,

在您的视图中使用 {{.}}。这是指模板中引用的列表中的当前项。当您使用字符串数组而不是对象文字时,会显示数组的内容。如果您使用以前的对象文字变量,[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>