Last Updated: February 25, 2016
·
1.501K
· 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.

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside