我正在从模板助手返回一个将其他对象嵌入到任意深度(表示目录树)的对象,我想将它作为嵌套列表呈现到我的模板中。由于空格键的原理是将逻辑排除在模板之外,它所提供的只是循环和if语句。这适用于迭代大多数对象和数组,甚至是嵌套到已知深度的对象,但对于任意深度的对象,这是不够的。我想出了一些不同的可能策略,我觉得这些策略都不是特别干净:
我必须在这里忽视一些事情。有什么想法吗?
答案 0 :(得分:5)
为什么不使用递归模板?
<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" }
]
}
}
})