How to render a Meteor Template from Collection of Template names?

I have three simple Templates in Meteor, and a Collection on the server with any combination of their names. I want to be able to render these templates dynamically based on which of their names are in the Collection.

Currently I am trying to accomplish this by using the client to subscribe to the Collection, and access the names through a template function. Unfortunately, if I try to run ">" on the names, Meteor attempts to render the variable name instead of the Template pointed to by its value.

So instead of rendering the html in template1, template2, and template3, the output is merely their names on the page: "template1 template2 template3".

Here is the code I've been using, I hope there's a way to solve my issue without having to run Meteor.render manually.

Server js:

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

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

Client 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>

You can make a render helper:

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

And use it with

{{render templateName}}

You might want to try this

in your 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>

in your script

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

I cannot comment (reputation not high enough :( ) ... but ...

I found this answer after searching around for quite a while for an answer to the following question:

"How to dynamically or programmatically select a handlebars template to render in meteor".

Basically, I want to have a collection of types of a Thing, say "a", "b" and "c" and, while iterating through a collection of such things, I want to select a type-specific template to render for each one ... where the templates are named "aThingItem", "bThingItem" and "cThingItem".

My solution is based on Tom Coleman's answer given above and I only needed to tweak it slightly to make it work for my situation. Thanks Tom for your answer!

I put this "answer" here only to (hopefully) help others who are trying to search for answers to questions worded similarly to mine.

链接地址: http://www.djcxy.com/p/66480.html

上一篇: 用于多处理的python调试工具

下一篇: 如何从模板名称集合渲染流星模板?