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

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

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

问候,

最佳答案

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

引用:https://github.com/janl/mustache.js/

对象

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

查看

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

输出

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

关于html - 如何获取 Mustache 循环表示中数组的根值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39894891/

相关文章:

javascript - React - 使用 bootstrap 创建 Accordion 不会在点击时展开/关闭

javascript - 如何使用 Mustache 在服务器端显示 json 数组中的嵌套 json 对象

javascript - 尝试显示数据时出现 mustache 循环错误

template-engine - 模板引擎如何工作?

html - 使用 calc() 容器将宽度设置为固定的 div

javascript - 如何在 javascript 中创建网站图标?

java - 使用 Java 生成的内容向 Outlook 发送电子邮件

javascript - Node js 和 mustache

java - Velocity:隐藏模板中的方法

backbone.js - 是否可以在模板中传递 cid 字段?