下划线模板(二合一)/ Backbone

时间:2013-05-06 18:09:01

标签: javascript jquery backbone.js underscore.js

如何在Underscore.js模板中从模板中调用其他模板?

HTML code:

<*script id="editWrapView" type="text/template">
        <div class="wrapperFields__leftUnderLine disp_intop"></div>
                <div class="wrapperFields__titleItem disp_intop">
                <span class="f_s_100"><%=title%></span>
                </div>
                <div class="wrapperFields__rightUnderLine disp_intop"></div>

                <div class="wrapperFields__basicFields ">
                    <ul class="basicFields__ulList">
                        <li class="ulList__item">
                            <span class="question" title=""></span>
                            <label id="sortOrder" class="labelItem">SortOrder</label>
                            <input name="sortOrder" class="custom-input width_fields_input"/>
                        </li>
                        <li class="ulList__item">
                            <span class="question" title=""></span>
                            <label id="sortOrder" class="labelItem">TYPE</label>
                            <select class="width_fields_select">
                                <option value="1" <% if(type == 1){ %>selected<%}%> >Category</option>
                                <option value="2" <% if(type == 2){ %>selected<%}%>>Link</option>
                                <option value="3" <% if(type == 3){ %>selected<%}%>>Spacer</option>
                                <option value="4" <% if(type == 4){ %>selected<%}%>>Divider</option>
                                <option value="5" <% if(type == 5){ %>selected<%}%>>HTML</option>
                            </select>
                        </li>
                    </ul>
                </div>
                <div class="wrapperFields__changeFields disp_intop">
                    <%= _.template( $("#category_"+type).html(), this) %> <!--  QUESTION STRING -->
                </div>
            </*script>
            <*script id="category_1" type="text/template">
                <ul class="basicFields__ulList" id="1">
                    <li class="ulList__item">
                    <span class="question" title=""></span>
                    <label id="sortOrder" class="labelItem">ChooseCategory</label>
                    <select class="width_fields_select">
                        <option value="1" <% if (type == 1){ %>selected <% } %> >Category</option>
                        <option value="2">Link</option>
                        <option value="3">Spacer</option>
                        <option value="4">Divider</option>
                        <option value="5">HTML</option>
                    </select>
                    </li>
                    <li class="ulList__item">
                    <span class="question" title=""></span>
                    <label id="sortOrder" class="labelItem">LEVELS TO SHOW</label>
                    <input name="sortOrder" class="custom-input width_fields_input"/>
                    </li>
                </ul>
                </*script>

主干代码:

render : function(){
            var tpl = _.template($("#editWrapView").html(), this.model.toJSON());
            this.$el.html(tpl);
            return this;
        },

1 个答案:

答案 0 :(得分:1)

您可以渲染部分模板并将其添加到主模板中。

尝试这样的事情:

render: function(){
     var tpl = _.template($("#editWrapView").html(), this.model.toJSON());
     this.$el.html(tpl);

     var partial = _.template($("#myPartial").html(), this.model.toJSON());
     this.$el.find(".wrapperFields__changeFields").append(partial);

     return this;
}