【问题标题】:Referring to the same template引用同一个模板
【发布时间】:2017-07-16 18:11:50
【问题描述】:

这就是我呈现模板的方式(点击页面的某些元素)

$("[entity]").click(function(e) {
   var entityType = $(this).data('type')
   var source   = $(`#${entityType}Tmpl`).html();
   var tmpl = $.templates(source);
   $("#details").html(tmpl.render(getData(entityType));
});

我想知道是否有一种 jsRender 方法可以为两个 entityType 定义单个模板,例如:

<script id="entity1;entity2" type="text/x-jsrender">type: {{:entityType}} </script>

【问题讨论】:

    标签: jsrender


    【解决方案1】:

    您可以将 entityType 作为助手传入。

    var helplers = {
      type: entityType,
      getValue: myGetValFunction,
      ...
    };
    
    var html = tmpl.render(getData(entityType), helpers);
    
    $("#details").html(html);
    

    然后根据类型让你的模板渲染输出:

    <script id="tmpl" type="text/x-jsrender">
      type:
      ... 
      {{if ~type==='foo}}...{{/if}}
      ...
      {{:~getValue(~type)}}
      ...
    </script>
    

    http://www.jsviews.com/#helpers

    【讨论】:

    • 我的数据中已经有 entityType,事实上我有几个模板(每个 entityType 1 个),唯一的例外是 2 entityType 应该指向同一个模板,没有其他方法可以检查模板内的值(或检查点击处理程序)?
    • 我不清楚你的意思是什么,或者你想解决什么问题。您可以尝试提供一个 jsfiddle 骨架示例,显示您的数据结构以及您如何调用模板 - 并阐明问题所在。在模板中,您可以检查您传递给它的数据,并且您可以通过将它们作为帮助程序传递来检查您需要的任何其他值。你还需要什么?!
    猜你喜欢
    • 2019-12-05
    • 1970-01-01
    • 1970-01-01
    • 2018-05-14
    • 2015-10-27
    • 1970-01-01
    • 2011-12-26
    • 2021-11-14
    • 1970-01-01
    相关资源
    最近更新 更多