带有 jsRender 的包含模板中当前循环的当前上下文
Current context for current loop in included template with jsRender
在 props
循环级别,我必须处理数据集:set_of_data_1
和 set_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}}
那么如何在模板B中打印~set_of_data_2
的#index
和#data
(#data.some_key
)?
更新:抱歉我的错误,浪费了鲍里斯的时间。现在我遇到了另一个问题(与上面的示例相同,但略有不同):
//TemplateA
{{props object.value}}
<span>{{:key}}</span>
<div>
{{for prop.set_of_data_1 ~prop=prop}}
{{include tmpl="TemplateB"/}}
{{/for}}
</div>
{{/props}}
//TemplateB
{{for ~prop.set_of_data_2}}
// This is working...
<span>{{:#parent.parent.data[#data.some_key]}}</span> //Accesing set_of_data_1
// But this doesn't...
{{if some_condition}}
<span>{{:#parent.parent.data[#data.some_key]}}</span> //Accesing set_of_data_1 inside IF (and I'm getting inside IF)
{{/if}}
{{/for}}
它对我来说似乎工作正常:
<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
这有什么问题吗?
在 props
循环级别,我必须处理数据集:set_of_data_1
和 set_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}}
那么如何在模板B中打印~set_of_data_2
的#index
和#data
(#data.some_key
)?
更新:抱歉我的错误,浪费了鲍里斯的时间。现在我遇到了另一个问题(与上面的示例相同,但略有不同):
//TemplateA
{{props object.value}}
<span>{{:key}}</span>
<div>
{{for prop.set_of_data_1 ~prop=prop}}
{{include tmpl="TemplateB"/}}
{{/for}}
</div>
{{/props}}
//TemplateB
{{for ~prop.set_of_data_2}}
// This is working...
<span>{{:#parent.parent.data[#data.some_key]}}</span> //Accesing set_of_data_1
// But this doesn't...
{{if some_condition}}
<span>{{:#parent.parent.data[#data.some_key]}}</span> //Accesing set_of_data_1 inside IF (and I'm getting inside IF)
{{/if}}
{{/for}}
它对我来说似乎工作正常:
<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
这有什么问题吗?