模板组成的把手帮手 [英] Handlebars helper for template composition
问题描述
用法如下:
> applyTemplate subTemplateId arg1 = 123 arg2 =abc...
也可以传递html内容
{{#applyTemplatetlia = 1 b =y}}
...此处的任何内容都会传递给子模板{{content} }
{{/ applyTemplate}}
这个jsFiddle演示了它的工作原理: http://jsfiddle.net/maxl/ywUjj/
我的问题:我希望调用作用域中的变量在子模板中的jsFiddle中可以访问
,请注意{{topLevelVar}}
不可用。
谢谢
在topLevelVar之前添加../
例如:
{{../ topLevelVar}}
< script id =tlitype =text / x-handlebars-template>
< tr>< td> {{a}} ----> {{content}}< ----- {{b}}< / td>< / tr>
< / script>
< script id =zazatype =text / x-handlebars-template>
< table>
{{#applyTemplatetlia = 1 b =y}}< input type =textvalue ='a'> {{../ topLevelVar}} {{/ applyTemplate}}
{{#applyTemplatetlia = 2 b =z}}< input type =textvalue ='b'> {{/ applyTemplate}}
< / table> ;
< / script>
I have a Handlebar helper to invoke a template within a template,
the usage is this :
applyTemplate subTemplateId arg1=123 arg2="abc" ...
It is also possible to pass html content
{{# applyTemplate "tli" a=1 b="y"}}
... any content here will get passed to the sub template with {{content}}
{{/ applyTemplate }}
This jsFiddle illustrates how it works : http://jsfiddle.net/maxl/ywUjj/
My problem : I want the variables in the calling scope to be accessible in the sub templemplate, in the jsFiddle, notice how {{topLevelVar}} is not available.
Thanks
Add "../" before topLevelVar to access the parent context.
For example: {{../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>
这篇关于模板组成的把手帮手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!