With a template how to use {{attribute}} with one 'record' compared to #each using a find() cursor?

meteor

Solution

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")});
}

Problem

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 UPDATE 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: ""}; } ```

Original source