admin管理员组文章数量:1425734
I am trying to learn mustache / icanhaz in conjunction with jquery and javascript, and I've got a mustache template to which I'm passing various data. One of the pieces of data is a list of choices, but that list can vary in length (say, one to three choices). How do I pass that varying data to mustache?
This is my code:
Javascript:
for (childIndex in scenes[sceneID].children) {
childSceneID = scenes[sceneID].children[childIndex];
childScene = scenes[childSceneID];
childLink = childScene.name;
}
decision = ich.decision(decisionData);
$('#page_container').append(decision);
Template:
<script id="decision" type="text/html">
<div id="page">
<h1>{{ tTitle }}</h1>
<ul id="options">
<li>{{tDecision}}</li>
</ul>
{{#tBacklink}}<a id="back" data-sceneid="{{tBacklink}}">Back</a>{{/tBacklink}}
</div>
</script>
So somehow I have to pass all the childLinks in the decision object to mustache to be parsed in a loop to output the list of <li>
elements.
Anybody know how to do this?
I am trying to learn mustache / icanhaz in conjunction with jquery and javascript, and I've got a mustache template to which I'm passing various data. One of the pieces of data is a list of choices, but that list can vary in length (say, one to three choices). How do I pass that varying data to mustache?
This is my code:
Javascript:
for (childIndex in scenes[sceneID].children) {
childSceneID = scenes[sceneID].children[childIndex];
childScene = scenes[childSceneID];
childLink = childScene.name;
}
decision = ich.decision(decisionData);
$('#page_container').append(decision);
Template:
<script id="decision" type="text/html">
<div id="page">
<h1>{{ tTitle }}</h1>
<ul id="options">
<li>{{tDecision}}</li>
</ul>
{{#tBacklink}}<a id="back" data-sceneid="{{tBacklink}}">Back</a>{{/tBacklink}}
</div>
</script>
So somehow I have to pass all the childLinks in the decision object to mustache to be parsed in a loop to output the list of <li>
elements.
Anybody know how to do this?
Share Improve this question edited Aug 15, 2011 at 21:45 Mrchief 76.3k20 gold badges145 silver badges192 bronze badges asked Aug 15, 2011 at 21:34 mheaversmheavers 30.2k63 gold badges201 silver badges326 bronze badges1 Answer
Reset to default 5Model your data in an object first of all,
var scene = {
tTitle: '',
tDecision: '',
tBacklink: ''
};
Then place each of these objects in an array on each iteration of your loop
var scenes = [];
for () {
scenes.push(scene);
}
Then call Mustache to render the template with the scenes array, the template has been modified like this
<script id="decision" type="text/html">
<div id="page">
{{#scenes}}
<h1>{{ tTitle }}</h1>
<ul id="options">
<li>{{tDecision}}</li>
</ul>
{{#tBacklink}}<a id="back" data-sceneid="{{tBacklink}}">Back</a>{{/tBacklink}}
{{/scenes}}
</div>
</script>
本文标签: jqueryjavascript and mustachepassing an array to templateStack Overflow
版权声明:本文标题:jquery - javascript and mustache - passing an array to template - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745441808a2658480.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论