Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Do I Define a Global Template Helper Function?

In many templates I want to use the same functions, but they must defined in every template. like this:

function getNodesById(id){
    return collection.find({sid:id}).fetch();
}

Template.navigation.getNodesById= function(id){
    return getNodesById(id);
}

Template.body.getNodesById= function(id){
    return getNodesById(id);
}

Html:

<Template name="navigation">
... 
{{#each getNodesById '1'}}
...
{{/each}}
...
</Template>
<Template name="body">
...
{{#each getNodesById '1'}}
...
{{/each}}
...
</Template>
...
<Template name="...">
 .....
</Template>

Are There any way can defined globle template function instead of a template ? just like it: In javascript:

    defined global tempele.functionA = function(...){
         return ...
    }

in html:

<Template name ="a">
   {{#each  functionA ...}}
   {{/each }}
</Template>

<Template name ="b">
   {{#each  functionA ...}}
   {{/each }}
</Template>
<Template name="...">
    {{ #..  functionA ...}}
        ....
     {{/...}}
</Template >

Can I do this? I hope I described the problem clearly.

like image 806
L.T Avatar asked Mar 11 '13 03:03

L.T


People also ask

How do you write a helper function in JavaScript?

To implement the helper, we write a JavaScript function that takes its arguments as an array. This is because helpers can also receive named arguments, which we'll discuss next. import { helper } from '@ember/component/helper'; function substring(args) { let [string, start, end] = args; return string.

What are helper functions?

What's a helper function? A helper function is a function that performs part of the computation of another function. Helper functions are used to make your programs easier to read by giving descriptive names to computations. They also let you reuse computations, just as with functions in general.


3 Answers

You can register your helpers with handlebars directly. This is what I am using for displaying the current users's email address:

Handlebars.registerHelper('currentUserName', function () {
    var user = Meteor.user();
    if (_.isUndefined(user) || _.isNull(user)) {
        return new Handlebars.SafeString("<i class='icon-spin icon-spinner'></i> Login");
    }
    return user.emails[0].address;
});

In any template I just call {{currentUserName}}. For you that'd be

Handlebars.registerHelper('getNodeById', function (id) {
    return collection.find({sid:id}).fetch();
});

As a side note: looking at how you want to use it, you might have gotten the idea of Meteor wrong. Meteor is data-driven - don't try to enforce flow-driven paradigms. If you are missing some data in your templates you should change the data-source, not just fetch it in your templates.

like image 158
Fge Avatar answered Oct 23 '22 17:10

Fge


As of Meteor 1.0, the documentation here instructs developers to use Template.registerHelper in order to define globally-available template helpers.

So in the case of this question, the correct code format would be this:

    Template.registerHelper("getNodesById", function(id) {
        return collection.find({sid: id});
    }

You could then reference this template helper in any of your templates in the following two ways:

    {{getNodesById '1'}}

or

    {{#each getNodesById '1'}}
      ...
    {{/each}}
like image 28
Keith Dawson Avatar answered Oct 23 '22 18:10

Keith Dawson


For Meteor 0.8 or higher, using UI.registerHelper will do the job.

like image 24
Archy Will He 何魏奇 Avatar answered Oct 23 '22 18:10

Archy Will He 何魏奇