我的部分模板:
<script id="teamspeak_channel_recursive" type="text/x-handlebars-template">
{{#each childChannel}}
<tr class="viewer-table-row">
<td class="viewer-table-cell">
<span></span>
<span class="viewer-label">{{name}}</span>
<span></span>
</td>
</tr>
{{#if childChannel}}
{{> teamspeak_channel_recursive}}
{{/if}}
{{/each}}
</script>
我想根据deph via css margin left或css class来设计名称。我也试过使用参数,但不允许计算数字或完成任何数学运算。数学助手也没有帮助。据我所知,模板中的Javascript是禁止的。
<script id="teamspeak_channel_recursive" type="text/x-handlebars-template">
{{#each childChannel}}
<tr class="viewer-table-row">
<td class="viewer-table-cell">
<span></span>
<span class="viewer-label">{{name}}</span>
<span></span>
</td>
</tr>
{{#if childChannel}}
{{> teamspeak_channel_recursive deph=({{../deph}}+1) }} <- dosen't work only statc values or the context itself is working
{{/if}}
{{/each}}
</script>
简而言之,我被困住了,除了使用有序列表并将显示模式设置为tablecell之外,我找不到出路。但这不是我想要的。同时迭代上下文以添加递归级别也不是一个不错的选择。
模型(不完整):
type": "channel",
"childChannel":
[
{
"type": "channel",
"childChannel":
[
{
"type": "channel",
"childChannel": null,
"childClient": null,
"name": "AFK Area"
}
],
"childClient": null,
"name": "WoW / SWToR / GuildWars2 hype"
},
{
"type": "channel",
"childChannel":
[
{
"type": "channel",
"childChannel": null,
"childClient": null,
"name": "Rumidler (AFK)"
}
],
"childClient": null,
"name": "FPS CS:GO Hype"
}
],
"childClient": null,
"name": "Hall of Games"
答案 0 :(得分:0)
我怀疑使用Handlebars Partials无法做到这一点。递归Helper可以做到这一点。我已经使用了你之前链接的小提琴作为最小概念证明的基础here。您可以更新以使用您自己的数据结构和HTML,但它基本上如下所示:
var listHelperOutput = '';
function recursiveList(stuff, depth){
listHelperOutput += '<ul>';
stuff.forEach(function(el){
listHelperOutput += '<li>';
listHelperOutput += el.name + ' (depth ' + depth + ')';
if (el.items){
recursiveList(el.items, depth + 1);
}
listHelperOutput += '</li>';
});
listHelperOutput += '</ul>';
return new Handlebars.SafeString(listHelperOutput);
}
Handlebars.registerHelper('listHelper', recursiveList);
在模板中调用它:
{{listHelper items 1}}