peerlibrary / meteor-blaze-components

Reusable components for Blaze
http://components.meteorapp.com/
BSD 3-Clause "New" or "Revised" License
354 stars 26 forks source link

blaze components won't load via custom render template function #96

Closed Alino closed 9 years ago

Alino commented 9 years ago

I have this global helper so I can load templates if they exists and do nothing if they don't: (idea from this article)

Template.registerHelper('render', function () {
    return Template[this.name] ? Template[this.name] : null;
});

but if I call a blaze component template with this approach the template is loaded but blaze component functionality is not. Is there any workaround on how to make this work please?

mitar commented 9 years ago

You should probably not be using this approach with Blaze Components. If you move to Blaze Components, it is easier and better to have a template helper on your base class as a method, which will then be available to all your child components.

You cannot directly access Template. Read the section in the README about what you are doing.

But in your case the easiest way to do it is to just do:

Template.registerHelper('render', function () {
    var template = BlazeLayout._getTemplate(this.name, Template.instance);
    return template || null;
});

BTW, this is really bad because it will recreate a template instance/component every time the data context of your helper changes. This is really not efficient and this is why global helpers as well are discouraged, especially for such things where you are creating templates/components.

You might be interested in looking into #21, which has a patch to make this || null unnecessary.

Alino commented 9 years ago

thank you again, I appreciate your answer, I will stop using this approach.