我正在从模板助手返回一个将其他对象嵌入任意深度(表示目录树)的对象,我想将其作为嵌套列表呈现到我的模板中.由于空格键的原理是将逻辑排除在模板之外,它所提供的只是循环和if语句.这适用于迭代大多数对象和数组,甚至是嵌套到已知深度的对象,但对于任意深度的对象,这是不够的.我想出了一些不同的可能策略,我觉得这些策略都不是特别干净:
我必须在这里忽略一些东西.有任何想法吗?
为什么不使用递归模板?
<template name="tree">
{{value}}
<ul>
{{#each children}}
<li>{{> tree}}</li>
{{/each}}
</ul>
</template>
<template name="container">
{{> tree treeData}}
</template>
Template.container.helpers({
treeData: function () {
return {
value: "level 1",
children: [
{ value: "level 2" },
{ value: "level 2" },
{ value: "level 2" }
]
}
}
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
371 次 |
| 最近记录: |