Ok, I know this is super basic, but I've been staring at it for 2 days and can't see why it is not working. I am using Handlebars IF helpers to conditionally render a template.
Here is the HTML:
<head>
<title>flash</title>
</head>
<body>
{{#if isTrue}}
{{> hello}}
{{else}}
{{> goodbye}}
{{/if}}
</body>
<template name="hello">
<h1>Hello!</h1>
</template>
<template name="goodbye">
<h1>Goodbye!</h1>
</template>
Here is the simple coffee file:
isTrue = true
I expect the {{> hello}} template to render, but no luck. I just get the {{> goodbye}} template. It's odd since I have other projects where I have done this successfully. I must be missing something obvious here.
The isTrue
variable needs to be in a template for it to work. So, put the body contents in a template:
<body>
{{> body}}
</body>
<template name="body">
{{#if isTrue}}
{{> hello}}
{{else}}
{{> goodbye}}
{{/if}}
</template>
And then you can define isTrue
like this:
Template.body.helpers
isTrue: -> true
Note:
Template.body.isTrue = -> true
is now deprecated.
The new syntax looks like this:
Template.test.helpers({
'isTrue': function(){
return true;
}
});
It should still work but if you open your console it will give you a warning about the syntax.
with Meteor 1.2.0.2 you can do it like this
Template.hello.helpers({
isTrue() { return true }
});
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