Blaze Template.contentBlock with default value?


I want to create a blaze template which can be used to wrap other stuff by using Template.contentBlock. But I want it to have a default value if it isn’t used as a wrapper:

The wrapper-template would look like:
{{#if Template.contentBlock}} {{> Template.contentBlock}} {{else}} <div>default</div> {{/if}}

But the if-clause don’t work like this, as it is always true – so is there a simple way to achieve this?


Use an helper so you can either provide a default value or feed the real value to the block.

{{#if helper}}
{{> Template.contentBlock}}

Hm. How can I invoke contentBlock within the helper to see if I got content passed or not?

Okay, I think I found it:
Within the helper I can call:

So I created a global helper:
Template.registerHelper('hasChild', function () { return (Template.instance().view.templateContentBlock) ? true : false; });

No I can use it like:
{{#if hasChild}} {{> Template.contentBlock}} {{else}} <div>default</div> {{/if}}

Thx for helping!

ok that works :slight_smile: but you are using stateful components, which means that components internally store the state on which rendering depends.

Try to do stateless now, which means that you pass the state as a variable, so it’s easier to reason about the application.