I know using a Template you can display multiple documents with their attributes like:
// html
<template name="hello">
{{#each greetings}}
{{message}}
{{/each}}
</template>
// js
Template.hello.greetings = function() {
return Greetings.find();
}
Which shows Greeting.message for each greeting found document.
My question is how to use this template for only one document? (incl. no available document)
From javascript side I would use something like return Greetings.findOne({'id' : Session.get("greeting_id")});
But when using the template:
<template name="hello">
{{message}}
</template>
an error is thrown: Uncaught TypeError: Cannot read property 'message' of undefined
For now I use this on JS side, using the template as suggested by @tom-wijsman below:
Template.hello.greeting = function() {
var greeting = Greetings.findOne({'id' : Session.get("greeting_id")})
if (greeting)
return greeting;
return {message: ""};
}
Handlebars.js also has a #with helper.
<template name="hello">
{{#with greeting}}
{{message}}
{{/with greeting}}
</template>
Template.hello.greeting = function() {
return Greetings.findOne({'id' : Session.get("greeting_id")});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With