Is there a way to pass a parameter to a Handlebars template in Meteor?

I am using Meteor 0.5.2 and trying to solve what seems to me to be an obvious model that should have an easy solution.

I have a template that should return different pieces of data in each instance of the template.

For example, I have a template showing a television program - showing a show.

Then I need to display two instances of the same template with different data - one with past shows and one with upcoming shows.

So, I have a tv program template:

<template name="tv_program"> {{#each shows}} ... </template> 

Ideally, I would like to do something like:

 {{> tv_program past_shows}} ... {{> tv_program upcoming_shows}} 

pass the parameter to the tv_program template instance, which I can read from JavaScript and configure mongo requests.

I have currently copied / pasted my / js template code and adjusted mongo requests, but there should be a better way.

I was looking for partial / helpers with arguments, but this does not seem to solve my problem.

Thank you Vladimir

+6
source share
2 answers

You are approaching the problem at the wrong level. Instead of trying to tell your code what to do with your templates, you should tell your template what to do with your code. For instance:

 Template.past_shows.shows = function() { return shows(-1); } Template.upcoming_shows.shows = function() { return shows(1); } function shows(order) { return Shows.find({}, {$sort: order}); } <template name="past_shows"> {{#each shows}} {{> show}} {{/each}} </template> <template name="upcoming_shows"> {{#each shows}} {{> show}} {{/each}} </template> <template name="show"> <li>{{_id}}: {{name}}</li> </template> 
+6
source

If you use a state machine for your application, for example meteor-router , you can use one template and fill it with past shows depending on the state of the application.

0
source

All Articles