【发布时间】:2016-12-12 08:13:13
【问题描述】:
是否可以从传递给顶级模板的上下文中渲染模板,甚至只是部分模板?看起来这可能需要递归渲染,但也许我遗漏了一些东西。
下面的示例使用 Bootstrap 演示了这一点。
说这是我的顶级模板:
<div class="panel">
<div class="panel-body">
{{{description}}}
</div>
</div>
我的上下文是:
{
description: "\
Some text before the warning.\
<div class=\"alert alert-warning\" role=\"alert\">\
<span class=\"glyphicon glyphicon-warning-sign\" aria-hidden=\"true\"> </span>\
My warning here.\
</div>\
Some text after the warning."
}
出于多种原因,我想做的是将警报分成部分:
- 在周围文本中任意放置
- 可以为警告以外的类型(危险、信息等)制作部分内容
- 可以根据需要添加任意数量的散布在上下文字符串中
由于这些原因,似乎无法将其放入顶级模板中。
部分看起来像这样:
<script id="partial-warning-template" type="text/x-handlebars-template">
<div class="alert alert-warning" role="alert">
<span class="glyphicon glyphicon-warning-sign" aria-hidden="true"> </span>
{{{warning-message}}}
</div>
</script>
一旦到位,我就可以像这样使用它:
{
description: "\
Some text before the warning.\
{{> partial-warning-template \"My warning here.\"}}\
Some text after the warning.\
{{> partial-warning-template \"Now adding a second warning.\"}}"
}
也许我遗漏了一些基本的东西 - 有没有更惯用的方法来做到这一点?
【问题讨论】:
标签: handlebars.js