2013-02-12 134 views
0

我使用的是当前最新的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")}} 

任何人都有线索,或者(鲍里斯)可能会让它进入下一个修订版吗?

+0

对于第一部分,您可以轻松地做到这一点。树标签控件示例演示了对标签的递归调用。您也可以递归调用,并传递不同的参数。 对于第二部分,是的,有一个用于关联模板的功能,然后让模板调用(包装)块内容。这将在下一次更新中,我会指出你的演示,然后... – BorisMoore 2013-02-17 22:25:32

+0

感谢鲍里斯。您是否打算允许模板包含文字*和*链接模板,或者只是一个{{tmpl =}}样式库调用可以嵌入到文字内容中?后一种方法对我来说似乎更清楚。 – cirrus 2013-02-18 10:40:05

+0

嗨卷云 - 不知道如果我明白你的替代​​风格。但最好的情况是,如果你能看看我的答案中提到的新JsRender功能(以及使用它的链接示例),并看看它是否适合你的场景... – BorisMoore 2013-02-27 17:59:29

回答

1

随着最新JsRender更新,现在提供两种模板和块内容的新的“包裹模板”功能,从而使模板可以再“包装”的内容。 举例来说,参见http://borismoore.github.com/jsrender/demos/step-by-step/06_template-composition.html

+0

是的,这看起来不错。我发现你可以选择文字内容的出现位置,有点像传递一个参数。那么#content是一个保留的模板名称?你可以传递其他参数来包含方法吗? – cirrus 2013-02-27 22:21:59

+0

#content是视图对象的内容属性:view.content。 #foo是view.foo的缩写。您注意到它不是一个字符串“#content” - 所以不会与命名模板发生冲突,该命名模板由字符串名称引用。 – BorisMoore 2013-03-01 18:06:02

+0

至于传递参数,您可以编写{{include_myparam = expr tmpl = ... /}},然后在包含的内容中访问〜参数,例如。如果您正在使用tmpl =#内容,则包装的块。因此,您可能有{{wrapper}} {{:〜myparam}} {{/ wrapper}},其中wrapper标记将myparam作为参数传递给其封装内容{{include ... /}} – BorisMoore 2013-03-01 18:15:49

0

所以这里是我如何使用递归模板解决问题的第一部分。根据通过的参数设置L1,L2,L3等;

<script id="tmplQ" type="text/x-jquery-tmpl"> 
    <div class="selected stmQ"> 
     {{for process.qs[0]}} 
      {{for cq}} 
       <a id="q-{{:stm.n}}">q-{{:stm.n}}</a> 
       <div class="table"> 
        {{for cq ~depth=1 tmpl="#tmplNestedQ"/}} 
       </div> 
      {{/for}} 
     {{/for}} 
    </div> 
</script> 

<script id="tmplNestedQ" type="text/x-jquery-tmpl"> 
    <div class="rgRow L{{:~depth}} e"> 
     <div class="td q">{{:q}}</div> 
    </div> 
    {{for cq ~depth=(~depth+1) tmpl="#tmplNestedQ"/}} 
</script>