Last Updated: February 25, 2016
·
1.289K
· guilhermedecampo

New way to create custom block helper Meteor 0.8

Hi guys!

Meteor 0.8 came with some awesome changes in the template engine called Blaze.

With the new engine a lot of things changed but of course for better!

For custom blocks helpers I mean the ones that are not build-in. So if you want to do a custom block helper you have to follow these steps:

The example is for an if block that shows content only if there is information in a given variable. This is good because we can reuse this code in other parts of our view. Don't make verbose code!

Create a template

<template name="ifAny">
  {{#if isAny data}}
    {{> UI.contentBlock}}
  {{else}}
    {{> UI.elseBlock}}
  {{/if}}
</template>

Create a function that deal with your variable (data)

Template.ifAny.isAny = function (data) {
 If (!data || (_.isArray(data) && !data.length) || (_.isFunction(data.fetch) && !data.count())){
    return false;
}else{
   return true;
}
};

Use it!

 {{#ifAny data=addressesList}}
 <p> Do this </p>
 {{else}}
 <p> Do that </p>
{{/ifAny}}

Thanks! Hope it helps someone...

If you have a better way please let me know.