下划线需要模板帮助 - 模板集合
我使用 underscore.js 进行模板化。这是一个示例模板。
<script id="discussion-template" type="text/html">
[[ _.each(discussions, function(topic){ ]]
<li>
<article id="{{ topic.htmlId() }}">
<a class="section-arrow mir" href="#">toggle</a>
<h3>{{ topic.get('text') }}</h3>
<ol></ol>
</article>
</li>
[[ }); ]]
</script>
在backbone.js view.render() 中,我将一个集合传递给模板。
this.el.append(this.template({ discussions: this.collection.models }));
我的问题是,我必须编写循环代码吗?我不能只传递一个集合,并且下划线足够聪明,可以为集合中的每个项目渲染一个项目吗? underscore.js 是否还为嵌套模板提供了一些东西?集合中的每个项目实际上都有一个我也需要渲染的项目集合。如何从此模板中调用另一个模板。当然,我们非常感谢任何链接、提示和/或教程。
谢谢!
I'm using underscore.js for templating. Here is a sample template.
<script id="discussion-template" type="text/html">
[[ _.each(discussions, function(topic){ ]]
<li>
<article id="{{ topic.htmlId() }}">
<a class="section-arrow mir" href="#">toggle</a>
<h3>{{ topic.get('text') }}</h3>
<ol></ol>
</article>
</li>
[[ }); ]]
</script>
Inside a backbone.js view.render() I'm passing a collection to the template.
this.el.append(this.template({ discussions: this.collection.models }));
My question here is , do I have to write the looping code? Can I not just pass in a collection and underscore be smart enough to render one item per item in collection? Also does underscore.js provide something for nesting templates? Each item in the collection actually has a collection of items I will need to render as well. How can I call another template from within this template. Any links, tips, and or tutorials are of course greatly appreciated.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您确实必须编写循环代码,但是您可以通过在视图而不是模板中使用循环来清理它。因此,您将拥有一个用于容器的模板(包含
),另一个用于呈现
。
对于作为项目集合的每个项目,您可以使用相同的技术,将这些模型附加到
中主题项模板。
我没有测试下面的代码,所以我不是 100% 它不是没有错误的,但它应该给你一个解决它的方法:)
I think you do have to write the looping code, but you could clean it up by having the loop in the view rather than the template. So you'd have one template for the container (that holds the
<ol>
) and another for rendering<li>
s.For the each item being a collection of items you can use the same technique, with those models appending to the
<ol class="topic-collection-will-append-to-this">
in the topic item template.I didn't test the code below so I'm not 100% it's not bug free, but it should give you idea of a way to tackle it :)
您可以有两个模板,一个用于列表,一个用于内部元素。在列表模板中,只需打印内部模板的结果: http://jsfiddle.net/dira/ hRe77/8/
Underscore 的模板非常非常简单,没有附加任何智能行为/魔法:http://documentcloud.github.com/underscore/docs/underscore.html#section -120
You can have two templates, one for the list and one for the inner elements. And in the list template just print the result of the inner one: http://jsfiddle.net/dira/hRe77/8/
Underscore's templates are very very simple and don't have any smart behaviour/magic attached: http://documentcloud.github.com/underscore/docs/underscore.html#section-120
您正在寻找的是一个更强大的模板系统。 Underscore 的模板非常少,没有内置的循环支持等。也许 Mustache 模板更适合您? (旁注:出于某种奇怪的原因,它被称为无逻辑。有了递归和 lambda 支持,我想说你至少已经完成了计划的一半,但我离题了..)
What you're looking for is a more competent templating system. Underscore's templating is very minimal, without built-in support for looping and such. Maybee Mustache templates is more for you? (Sidenote: it's called logic-less for some strange reason. With both recursion and lambda support I'd say you're at least half-way to Scheme, but I digress..)