【问题标题】:Compositing Multiple Templates in jTemplates在 jTemplates 中合成多个模板
【发布时间】:2010-09-12 21:54:46
【问题描述】:

使用 jTemplates,可以像这样合成模板...

<textarea id="templateList" class="template"><!--

  {#template RESULTS}
    This a template for generating a list of results
    {#include PAGINATION root=$T}
  {#/template RESULTS}

  {#template PAGINATION}
    This is a template for generating pagination through the results
  {#/template PAGINATION}

--></textarea>

但是,有时能够组合完全不同的模板会很方便。例如,我有很多不同类型的列表,每个列表都有不同的模板。使用上面的方法,我不得不在我的每个模板中为不同的列表一遍又一遍地重复相同的代码块进行分页。

我更愿意做如下的事情......

<textarea id="templateList" class="template"><!--
  This is a template listing results
  {#some kind of call to templatePagination}
--></textarea>

<textarea id="templatePagination" class="template"><!--
  This is a template for generating pagination
--></textarea>

有谁知道这样的事情是否可行,如果可以,如何去做?

谢谢!

【问题讨论】:

    标签: javascript jquery jtemplate


    【解决方案1】:

    如果您没有模板引用,您可以在setTemplate 时将所有需要的模板附加在一起。

    $(container)
       .setTemplate($('templateList').html() + $('templatePagination').html())
    

    或者您可以createTemplate 并将引用作为include 传递

    var t = $.createTemplate($('templatePagination').html());
    $(container).setTemplate($('templateList').html(), t._templates)
    

    【讨论】:

      【解决方案2】:

      你应该可以做到:

      var templates = $.createTemplate($('templateList').html())._templates;
      

      然后你可以使用模板并做:

      $('#SOMEDIV').setTemplate(templates['PAGINATION'],templates);
      &('#SOMEDIV').processTemplate(data);
      

      我是一个 js/jquery 新手,如果它不完美,很抱歉。我正在使用 $.createTemplateURL 而不是我们的文本区域从 url 加载它

      【讨论】:

        猜你喜欢
        • 2021-12-14
        • 2012-03-28
        • 2011-05-10
        • 1970-01-01
        • 2010-09-29
        • 1970-01-01
        • 2013-03-06
        • 2015-11-03
        • 1970-01-01
        相关资源
        最近更新 更多