admin管理员组文章数量:1311065
I want to use a nested template in different parts of my webpage. For the different parts I need to get a value from an array within the nested template. I cannot use a for loop because each part has different class and position on the website. Is it possible to pass a variable into the nested template? The following code simplifies what I am trying to achieve:
<script id="myBtnTmpl" type="text/x-jsrender">
<button class="btn">
{{:myData.myArray[INDEX_VARIABLE].btnName}}
</button>
</script>
// Here I want to use INDEX_VARIABLE = 0
<div class="BigButton">
{{if myData tmpl="myBtnTmpl"/}}
</div>
// Here I want to use INDEX_VARIABLE = 1
<div class="MediumButton">
{{if myData tmpl="myBtnTmpl"/}}
</div>
// Here I want to use INDEX_VARIABLE = 2
<div class="SmallButton">
{{if myData tmpl="myBtnTmpl"/}}
</div>
Another question: When using nested templates is it possible to include nested templates like this {{tmpl="myBtnTmpl"/}} without the if syntax?
Thanks!
I want to use a nested template in different parts of my webpage. For the different parts I need to get a value from an array within the nested template. I cannot use a for loop because each part has different class and position on the website. Is it possible to pass a variable into the nested template? The following code simplifies what I am trying to achieve:
<script id="myBtnTmpl" type="text/x-jsrender">
<button class="btn">
{{:myData.myArray[INDEX_VARIABLE].btnName}}
</button>
</script>
// Here I want to use INDEX_VARIABLE = 0
<div class="BigButton">
{{if myData tmpl="myBtnTmpl"/}}
</div>
// Here I want to use INDEX_VARIABLE = 1
<div class="MediumButton">
{{if myData tmpl="myBtnTmpl"/}}
</div>
// Here I want to use INDEX_VARIABLE = 2
<div class="SmallButton">
{{if myData tmpl="myBtnTmpl"/}}
</div>
Another question: When using nested templates is it possible to include nested templates like this {{tmpl="myBtnTmpl"/}} without the if syntax?
Thanks!
Share Improve this question asked Jul 4, 2012 at 8:17 doormandoorman 17k22 gold badges95 silver badges161 bronze badges 2- For second question, {{for myData tmpl="#myBtnTmpl"/}} But totally don't understand your original question.. – Willy Commented Jul 4, 2012 at 9:02
- Anyway, according to the title, I think you can have a reference at github./BorisMoore/jsrender/blob/master/demos/step-by-step/… – Willy Commented Jul 4, 2012 at 9:09
1 Answer
Reset to default 8Yes, you can set named template parameters on the tag where you are using tmpl="myBtnTmpl"
(whether that be an {{if}}
tag or a {{for}}
tag):
<div class="BigButton">
{{for myData ~arrIndex=0 tmpl="myBtnTmpl"/}}
</div>
Then you can access the template parameter in the same way you would access a registered helper - by appending '~' to the name.
<button class="btn">
{{:myData.myArray[~arrIndex].btnName}}
</button>
Incidentally, you can also pass variables and helper functions (in addition to the data) with the render method. I just added a new sample demo showing that.
So what this means is that templates can be 'parameterized' similarly whether you are rendering them from code, or declaratively as in your nested templates above.
本文标签: javascriptJsRender How to pass variables into a nested templateStack Overflow
版权声明:本文标题:javascript - JsRender: How to pass variables into a nested template - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741792561a2397746.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论