我使用的是当前最新的jsRender(截至2013年12月2日),我试图分解模板,以便可以呈现嵌套对象模型。嵌入一个没有{{for}}或组合内嵌+块模板的jsRender模板
我希望能够;
1.将一个参数传递给我的模板函数,以便我可以用不同的参数递归地调用它;
<script id="tmplQ" myParam="int?" type="text/x-jquery-tmpl">
<div class="rgRow L{:*myParam*} e"> // wrap this in L2/L3/L4 etc depending on myParam
<div class="td q">{{:q}}</div>
</div>
{{for cq templ="#tmlQ(myParam+1)"}} // increment parameter for recursing
<script>
所以基本上,我想在myParam通过以0开始与,然后在模板调用自身,因为它通过嵌套JSON对象模型下钻。好吧,所以搜索几页更远,它看起来像你可以这样做:JsRender: How to pass variables into a nested template但我仍然希望看到这些其他选项,如果可能的话; [/更新]
2.如果不行的话,我一直在试图简单地包括在另一联模板块模板的一部分;
{{for cq tmpl="#tmplQ"}} // renders inline template but nothing else
<div class="rgRow L2 e"> // needed to wrap this in L2
{{for cq tmpl="#tmplQ"}} // can't mix inline/block templates
<div class="rgRow L3 e"> // wanted to wrap this in L3
{{for cq tmpl="#tmplQ"/}} // would work if it got here
</div>
{{/for}}
</div>
{{/for}}
更简单的库模板;
<script id="tmplQ" type="text/x-jquery-tmpl">
<div class="td q">{{:q}}</div>
</script>
麻烦的是,jsRender似乎并不支持混合内嵌和块样式。只要您将tmpl=
置于{{for}}
中,它就会忽略嵌套在其下的所有内容。这是一个耻辱。我希望看到它支持两者的混合。它甚至不会抛出错误。
我也试图找到这样的语法来简单地调用模板内联。它存在吗?
{{for cq tmpl="#tmplQ"}} // renders inline template but nothing else
<div class="rgRow L2 e"> // wrap in L2
{{for cq}}
{{call tmpl="#tmplQ"}} // call library template????
<span>other content</span>
{{/for}}
</div>
{{/for}}
但它也不起作用。我也尝试过直接调用模板。
{{tmplQ()}}
{{tmpl("#tmplQ")}}
任何人都有线索,或者(鲍里斯)可能会让它进入下一个修订版吗?
对于第一部分,您可以轻松地做到这一点。树标签控件示例演示了对标签的递归调用。您也可以递归调用,并传递不同的参数。 对于第二部分,是的,有一个用于关联模板的功能,然后让模板调用(包装)块内容。这将在下一次更新中,我会指出你的演示,然后... – BorisMoore 2013-02-17 22:25:32
感谢鲍里斯。您是否打算允许模板包含文字*和*链接模板,或者只是一个{{tmpl =}}样式库调用可以嵌入到文字内容中?后一种方法对我来说似乎更清楚。 – cirrus 2013-02-18 10:40:05
嗨卷云 - 不知道如果我明白你的替代风格。但最好的情况是,如果你能看看我的答案中提到的新JsRender功能(以及使用它的链接示例),并看看它是否适合你的场景... – BorisMoore 2013-02-27 17:59:29