包含jsRender的模板中当前循环的当前上下文

时间:2015-07-18 23:02:38

标签: jsrender

props循环级别,我必须设置数据集:set_of_data_1set_of_data_2。 我想循环set_of_data_1,因为每次迭代都包含另一个模板,并且包含模板循环set_of_data_2

//TemplateA
{{props object.value}}
    <span>{{:key}}</span>
    <div>
    {{for prop.set_of_data_1 ~set_of_data_2=prop.set_of_data_2}}
        {{include tmpl="TemplateB"/}}
    {{/for}}
    </div>
{{/props}}


//TemplateB
{{for ~set_of_data_2}}
    // <- here I want to loop trough set_of_data_2,
    // but it seems that #index (actually #getIndex())
    // is resolving the #index of the for loop from TemplateA (prop.set_of_data_1)
    // and #data is resolving the indexes of ~set_of_data_2
{{/for}}

那么如何在TemplateB中打印#index的{​​{1}}和#data #data.some_key}?

更新:抱歉我的错误并浪费你的时间鲍里斯。现在我有另一个问题(与上面相同,但略有不同):

~set_of_data_2

1 个答案:

答案 0 :(得分:1)

它似乎对我有效:

<script type="text/x-jsrender" id="tmplA">
  {{props object.value}}
    <span>{{:key}}</span>
    <div>
    {{for prop.set1 ~set2=prop.set2}}
      <div><br/>
        One{{:val}}<br/> {{include tmpl="#tmplB"/}}
      </div>
    {{/for}}
    </div>
  {{/props}}
</script>

<script type="text/x-jsrender" id="tmplB">
  One{{:val}}
  {{for ~set2}}
    <div>
      Two{{:val}} TwoIndex{{:#index}}
    </div>
  {{/for}}
</script>

<div id="result"></div>

<script>
  var app = {
    object: {
      value: {
        foo: {
          set1: [{val: "aaa"}, {val: "bbb"}],
          set2: [{val: "AAA"}, {val: "BBB"}]
        }
      }
    }
  };

  var tmpl = $.templates("#tmplA");

  $("#result").html(tmpl.render(app));
</script>

结果:

foo 

Oneaaa
Oneaaa 
TwoAAA TwoIndex0 
TwoBBB TwoIndex1 


Onebbb
Onebbb 
TwoAAA TwoIndex0 
TwoBBB TwoIndex1 

这有什么问题吗?