【问题标题】:How to render a Meteor Template from Collection of Template names?如何从模板名称集合中渲染流星模板?
【发布时间】:2012-11-23 23:33:59
【问题描述】:

我在 Meteor 中有三个简单的模板,在服务器上有一个集合,它们的名称可以任意组合。我希望能够根据集合中的名称动态呈现这些模板。

目前我正在尝试通过使用客户端订阅集合并通过模板函数访问名称来完成此操作。不幸的是,如果我尝试在名称上运行 ">",Meteor 会尝试渲染变量名称而不是其值所指向的模板。

因此,不是在 template1template2template3 中渲染 html,而是输出只是它们在页面上的名称:“template1模板 2 模板 3"。

这是我一直在使用的代码,我希望有办法解决我的问题,而无需手动运行 Meteor.render。

服务器js:

TemplatesToRender = new Meteor.Collection("templatesToRender");

TemplatesToRender.insert({templateName: "template3"});
TemplatesToRender.insert({templateName: "template2"});

客户端html:

<body>
    {{#each templatesToRender}}
        {{> templateName}}           // meteor trying to render a template
                                     // called "templateName" instead of the 
                                     // variable inside templateName.
    {{/each}}
</body>

<template name="template1">
    <span>Template 1</span>
</template>

<template name="template2">
    <span>Template 2</span>
</template>

<template name="template3">
    <span>Template 3</span>
</template>

【问题讨论】:

    标签: meteor


    【解决方案1】:

    您可以创建一个render 助手:

     Handlebars.registerHelper('render', function(name, options) {
       if (Template[name])
         return new Handlebars.SafeString(Template[name]());
     });
    

    并与它一起使用

    {{render templateName}}
    

    【讨论】:

    • 这里的问题是内存管理,因为你调用了Template[name]()函数,他一遍又一遍地重新生成模板。这种方法很好,但是替换时不要正确删除模板。我对此进行了测试,您在插入和删除了几个 fiv 之后,您将获得 50 mb 的内存!
    【解决方案2】:

    你可能想试试这个

    在你的 html 中

    <body>
    
        {{> templateToRender}}
    
    </body>
    
    <template name="templateToRender">
    
        {{! use below to detect which template to render}}
    
        {{#if templateName "template1"}}
            {{> template1}}
        {{/if}}
    
        {{#if templateName "template2"}}
            {{> template3}}
        {{/if}}
    
        {{#if templateName "template3"}}
            {{> template3}}
        {{/if}}
    
    </template
    
    <template name="template1">
    
        <p>this is template1</p>
    
    </template>
    
    <template name="template2">
    
        <p>this is template2</p>
    
    </template>
    
    <template name="template3">
    
        <p>this is template3</p>
    
    </template>
    

    在你的脚本中

    Template.templateToRender.templateName = (which) ->
        # if user have a field like templateName you can do things like
        tmplName = Meteor.user().templateName
        # Session.equals will cause a template render if condition is true.
        Session.equals which, tmplName
    

    【讨论】:

      【解决方案3】:

      Meteor 1.0 今天刚刚发布,我只想在 2014 年更新它:)

      https://docs.meteor.com/#/full/template_dynamic

      {{> Template.dynamic template=template [data=data] }}
      

      示例用法:

      {{#each kitten}}
         {{> Template.dynamic template=kitten_type data=this }}
      {{/each}}
      

      【讨论】:

        猜你喜欢
        • 2014-08-02
        • 2015-08-07
        • 1970-01-01
        • 2013-11-16
        • 2012-06-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-13
        相关资源
        最近更新 更多