下划线需要模板帮助 - 模板集合

发布于 2024-12-10 15:25:27 字数 823 浏览 0 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冷弦 2024-12-17 15:25:27

我认为您确实必须编写循环代码,但是您可以通过在视图而不是模板中使用循环来清理它。因此,您将拥有一个用于容器的模板(包含

    ),另一个用于呈现

对于作为项目集合的每个项目,您可以使用相同的技术,将这些模型附加到

    中主题项模板。

我没有测试下面的代码,所以我不是 100% 它不是没有错误的,但它应该给你一个解决它的方法:)

window.TopicView = Backbone.View.extend({
    template: _.template($("#topic-template").html()),
    tag: 'li',
    className: 'topic',

    initialize: function() {
        _.bindAll(this, 'render');
    },

    render: function() {
        $(this.el).html(this.template({topic: this.model}));
        return this;
    }
});

window.DiscussionView = Backbone.View.extend({
    tagName: 'section',
    className: 'discussion',
    template: _.template($('#discussion-template').html()),

    initialize: function() {
        _.bindAll(this, 'render');
        this.collection.bind('reset', this.render);
    },

    render: function() {
        var $topics,
        collection = this.collection;

        $(this.el).html(this.template({}));
        $topics = this.$(".topics");
        this.collection.each(function(topic) {
            var view = new TopicView({
                model: topic
            });
            $topics.append(view.render().el);
        });

        return this;
    }
});

<script id="topic-template" type="text/html">
    <article id="{{ topic.htmlId() }}">
        <a class="section-arrow mir" href="#">toggle</a>
        <h3>{{ topic.get('text') }}</h3>
        <ol class="topic-collection-will-append-to-this">
        </ol>
    </article>           
</script>

<script type="text/template" id="discussion-template">
    ...
    <ol class="topics">
    </ol>
</script>

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 :)

window.TopicView = Backbone.View.extend({
    template: _.template($("#topic-template").html()),
    tag: 'li',
    className: 'topic',

    initialize: function() {
        _.bindAll(this, 'render');
    },

    render: function() {
        $(this.el).html(this.template({topic: this.model}));
        return this;
    }
});

window.DiscussionView = Backbone.View.extend({
    tagName: 'section',
    className: 'discussion',
    template: _.template($('#discussion-template').html()),

    initialize: function() {
        _.bindAll(this, 'render');
        this.collection.bind('reset', this.render);
    },

    render: function() {
        var $topics,
        collection = this.collection;

        $(this.el).html(this.template({}));
        $topics = this.$(".topics");
        this.collection.each(function(topic) {
            var view = new TopicView({
                model: topic
            });
            $topics.append(view.render().el);
        });

        return this;
    }
});

<script id="topic-template" type="text/html">
    <article id="{{ topic.htmlId() }}">
        <a class="section-arrow mir" href="#">toggle</a>
        <h3>{{ topic.get('text') }}</h3>
        <ol class="topic-collection-will-append-to-this">
        </ol>
    </article>           
</script>

<script type="text/template" id="discussion-template">
    ...
    <ol class="topics">
    </ol>
</script>
今天小雨转甜 2024-12-17 15:25:27

您可以有两个模板,一个用于列表,一个用于内部元素。在列表模板中,只需打印内部模板的结果: 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

墨洒年华 2024-12-17 15:25:27

您正在寻找的是一个更强大的模板系统。 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..)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文