How to display a Meteor template from a 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 visualize these templates dynamically based on which of their names is in the collection.

I am currently trying to accomplish this using a client to subscribe to a collection and access names through a template function. Unfortunately, if I try to run a ">" in the names, Meteor will try to display the variable name instead of the template pointed to by its value.

Therefore, instead of displaying html in templates1, template2 and template3, the output is simply their names on the page: "template1 template2 template3".

Here is the code I used, I hope there is a way to solve my problem without having to run Meteor.render manually.

Js server:

TemplatesToRender = new Meteor.Collection("templatesToRender"); TemplatesToRender.insert({templateName: "template3"}); TemplatesToRender.insert({templateName: "template2"}); 

Html client:

 <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> 
+7
source share
4 answers

You can create a render helper:

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

And use it with

 {{render templateName}} 
+4
source

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 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 
0
source

I can not comment (reputation is not high enough: () ... but ...

I found this answer after a long search to answer the following question:

"How to dynamically or programmatically select a handle template for rendering in a meteor."

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

My decision is based on Tom Coleman's answer above, and I only had to tweak it a bit to make it work for my situation. Thank you Tom for your answer!

I put this β€œanswer” here only (hopefully) to help others who are trying to find answers to questions formulated in the same way as mine.

0
source

Meteor 1.0 just came out today and I just want to upgrade it to 2014 :)

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

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

Usage example:

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

All Articles