Handlebars.js 用于模板合成的把手辅助对象
我有一个把手助手来调用模板中的模板 用法如下: applyTemplate子模板ID arg1=123 arg2=“abc” 也可以传递html内容Handlebars.js 用于模板合成的把手辅助对象,handlebars.js,Handlebars.js,我有一个把手助手来调用模板中的模板 用法如下: applyTemplate子模板ID arg1=123 arg2=“abc” 也可以传递html内容 {{# applyTemplate "tli" a=1 b="y"}} ... any content here will get passed to the sub template with {{content}} {{/ applyTemplate }} 此JSFIDLE演示了它的工作原理: 我的问题:我希望调用范围中的变
{{# applyTemplate "tli" a=1 b="y"}}
... any content here will get passed to the sub template with {{content}}
{{/ applyTemplate }}
此JSFIDLE演示了它的工作原理:
我的问题:我希望调用范围中的变量可以访问
在JSFIDLE的子模板中,注意{{topLevelVar}}
没有
从这个例子中,我想说的是,您可以使用fn访问helper方法中的上下文
applyTemplate:函数(上下文,fn){
for(var i=0,j=context.length;i
其中fn是内部“模板”部分,上下文是应用于它的模型 从上的解决方案开始,我们可以获得相同的结果,但使用内联模板:
<script id="topLevel" type="text/x-handlebars-template">
{{# defTpl "test1"}}
La plantilla <b>diu</b> {{part}},{{../topLevelVar}}
{{/ defTpl }}
{{# each sub }}
Iplant-->{{eTpl "test1" part=this}}--fi plant<br>
{{/each}}
</script>
并称之为:
var _template = Handlebars.compile($('#topLevel').html());
$('body').append(_template({topLevelVar:123, content:"cascading",sub:[45,30,12]}));
希望这有帮助:)在topLevelVar之前添加“./”以访问父上下文
例如:
{{../topLevelVar}}
<script id="tli" type="text/x-handlebars-template">
<tr><td>{{a}}----> {{content}} <----- {{b}}</td></tr>
</script>
<script id="zaza" type="text/x-handlebars-template">
<table>
{{# applyTemplate "tli" a=1 b="y"}}<input type="text" value='a'>{{../topLevelVar}}{{/ applyTemplate }}
{{# applyTemplate "tli" a=2 b="z"}}<input type="text" value='b'>{{/ applyTemplate }}
</table>
</script>
{{a}-->{{content}我找到了答案,请参见:
var _template = Handlebars.compile($('#topLevel').html());
$('body').append(_template({topLevelVar:123, content:"cascading",sub:[45,30,12]}));
<script id="tli" type="text/x-handlebars-template">
<tr><td>{{a}}----> {{content}} <----- {{b}}</td></tr>
</script>
<script id="zaza" type="text/x-handlebars-template">
<table>
{{# applyTemplate "tli" a=1 b="y"}}<input type="text" value='a'>{{../topLevelVar}}{{/ applyTemplate }}
{{# applyTemplate "tli" a=2 b="z"}}<input type="text" value='b'>{{/ applyTemplate }}
</table>
</script>