General Meteor Pattern Template

In meteor, I can install various template helpers, for example:

Template.story.title = function () { return "title"; }; <template name="story"> <h3>{{title}}</h3> <p>{{description}}</p> </template> 

This is great, but if I have many variables, I would not want to set them separately, I want to transfer the context to the main template.

How to do it?

 Template.story.data = function () { return {title:"title", description:"desc"}; }; <template name="story"> <h3>{{title}}</h3> <p>{{description}}</p> </template> 

This does not work. THANKS

+8
javascript meteor
source share
2 answers

You can set the context of the template when it is called:

 {{> story data}} Template.outerTemplate.data = function() { return {title:"title", description:"desc"}; } 

Or you can simply use {{#with}} to set the template context on the fly:

 {{#with data}} {{title}} {{/with}} 
+12
source share

You are absolutely on the right track, but you missed using your template variable the way you defined it. Since Template.story.data defined to return an object, you should use it as an object:

 <template name="story"> <h3>{{data.title}}</h3> <p>{{data.description}}</p> </template> 

Voila. Of course, each template variable can contain more than just a string.

+5
source share

All Articles