【问题标题】:How to combine many templates into one html in the underscore, text如何在下划线、文本中将多个模板组合成一个html
【发布时间】:2015-12-29 16:05:57
【问题描述】:

我在 tpl 文件夹中有很多模板。为了加快加载性能并减少与服务器的交互,我想将模板组合在一个带有 Id 标签的 HTML 中:

例如:

以下是模板common.html的内容

+++++++++++++++++++++++++++++++++++++++++++++++
<script type="text/template" id="tpl-banner1">
    <div class="banner">
        <div class="banWp addWrap" id="bannerWp1">
        </div>
    </div>
</script>
<script type="text/template" id="tpl-banner2">
    <div class="banner">
        <div class="banWp addWrap" id="bannerWp2">
        </div>
    </div>
</script>

+++++++++++++++++++++++++++++++++++++++++++++++

一个 html 文件中的两个部分。在 js 中,我只是通过以下方式引用模板:

define(['text!common.html'], function (IndexBannerTpl)  {

}

我的问题是如何引用该部分(id="tpl-banner2")?在骨干?

If only contains one section, I could use it like that:
+++++++++++++++++++++++++++++++++++++++++++++++
var template = _.template(IndexBannerTpl, {items: items_ibm});
+++++++++++++++++++++++++++++++++++++++++++++++

How to use id="tpl-banner1" and id="tpl-banner2"?

肖恩王

【问题讨论】:

    标签: javascript html templates backbone.js underscore.js


    【解决方案1】:

    尝试以下方法:

    var html = $(IndexBannerTpl).filter('#tpl-banner1').html();
    var template = _.template(html, {items: items_ibm});
    

    【讨论】:

    • var html = $(IndexBannerTpl).find('#tpl-banner1').html();查找不起作用。但过滤器可以。
    猜你喜欢
    • 2021-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多