Mustache - 以使用相同名称的嵌套部分为条件
Mustache - Conditional on nested sections using same names
我有一个类别节点(对象数组)。如果 product
字段存在,我想打印一个列表项,如果 category
字段存在,我想打印一个不同的列表项。
category.nodes = [
{
product: "1",
type: "product"
},
{
product: "2",
type: "product"
},
{
category: "categoryName",
type: "category"
}
]
我的小胡子模板是:
<p>
{{#category.nodes}}
{{#product}}
<li>{{product}}</li>
{{/product}}
{{#category}}
<li>{{category}}</li>
{{/category}}
{{/category.nodes}}
</p>
{{#category.nodes}} ... {{/category.nodes}}
应将范围放入 category.nodes section 和部分
"render blocks of text one or more times, depending on the value of the key in the current context"
文档在 false values and empty lists 上声明如下:
"If the person key does not exist, or exists and has a value of null, undefined, false, 0, or NaN, or is an empty string or an empty list, the block will not be rendered"
This jsFiddle 演示嵌套值与父值相同时的问题({{#category}}...{{/category}}
指的是全局 category
,而不是 category.nodes.category。)
我想知道是否有某种方法可以指定引用了哪个 category
,因为这会给嵌套带来问题。
This jsfiddle 是通过更改父名称来解决问题的方法,我想知道是否有某种方法可以指定引用哪个 category
,而不是重命名变量。
问题是当部分没有 category
时打印 {{#category}}...{{/category}}
。
此行为符合 the specification。
Walk the context stack from top to bottom, finding the first context
that is a) a hash containing the name as a key OR b) an object responding
to a method with the given name
当 {{#category}}
在 category.nodes
的成员中不匹配时,它遍历堆栈并在根部找到 category
(在此术语中位于堆栈的底部).根据我对规范的阅读,无法辨别数据元素的级别。
我有一个类别节点(对象数组)。如果 product
字段存在,我想打印一个列表项,如果 category
字段存在,我想打印一个不同的列表项。
category.nodes = [
{
product: "1",
type: "product"
},
{
product: "2",
type: "product"
},
{
category: "categoryName",
type: "category"
}
]
我的小胡子模板是:
<p>
{{#category.nodes}}
{{#product}}
<li>{{product}}</li>
{{/product}}
{{#category}}
<li>{{category}}</li>
{{/category}}
{{/category.nodes}}
</p>
{{#category.nodes}} ... {{/category.nodes}}
应将范围放入 category.nodes section 和部分
"render blocks of text one or more times, depending on the value of the key in the current context"
文档在 false values and empty lists 上声明如下:
"If the person key does not exist, or exists and has a value of null, undefined, false, 0, or NaN, or is an empty string or an empty list, the block will not be rendered"
This jsFiddle 演示嵌套值与父值相同时的问题({{#category}}...{{/category}}
指的是全局 category
,而不是 category.nodes.category。)
我想知道是否有某种方法可以指定引用了哪个 category
,因为这会给嵌套带来问题。
This jsfiddle 是通过更改父名称来解决问题的方法,我想知道是否有某种方法可以指定引用哪个 category
,而不是重命名变量。
问题是当部分没有 category
时打印 {{#category}}...{{/category}}
。
此行为符合 the specification。
Walk the context stack from top to bottom, finding the first context that is a) a hash containing the name as a key OR b) an object responding to a method with the given name
当 {{#category}}
在 category.nodes
的成员中不匹配时,它遍历堆栈并在根部找到 category
(在此术语中位于堆栈的底部).根据我对规范的阅读,无法辨别数据元素的级别。