Is it possible to call a template helper inside a template instantiation?
For my project, I have created templates for all my custom-styled input elements to make them re-usable. For example, the checkbox would look like:
<template name="myCheckbox">
<input type="checkbox" id="{{id}}" class="myCheckbox ..." />
</template>
To use it, I simply go:
{{> myCheckbox id="allowEdit"}}
This makes it easy for me to control the look and feel of my inputs across the entire project, as I only need to change the template in order to update all my checkboxes. This works fine, but now I need a template helper to add a "checked" attribute to my checkbox based on the DB. E.g.
Template.myTemplate.helpers({
checkAllowEdit: function() {
return (this.allowEdit) ? "checked" : "";
}
});
{{> myCheckbox id="allowEdit" {{checkAllowEdit}} }}
This doesn't work. Meteor doesn't like me trying to use a helper inside the instantiation. So my question is: Is there any way to call a template helper inside a template instantiation?
For checking a checkbox from a helper, you can use this Blaze feature: have your helper return a boolean and just assign it to your child template's checkbox checked
property.
Template.myTemplate.helpers({
checkAllowEdit: function() {
return Boolean(this.allowEdit); // Boolean cast may be unnecessary
}
});
<template name="myTemplate">
{{!-- things... --}}
{{> myCheckbox id="allowEdit" checked=checkAllowEdit }}
</template
<template name="myCheckbox">
<input type="checkbox" id="{{id}}" class="myCheckbox ..." checked={{checked}} />
</template>
A more natural way would be to use a helper in the myCheckbox
template instead of the myTemplate
one:
<template name="myTemplate">
{{!-- things... --}}
{{> myCheckbox id="allowEdit" }}
</template>
Template.myCheckbox.helpers({
checkAllowEdit: function() {
return Boolean(this.allowEdit); // Boolean cast may be unnecessary
}
});
<template name="myCheckbox">
<input type="checkbox" id="{{id}}" class="myCheckbox ..." checked={{checkAllowEdit}} />
</template>
But I have a feeling you want it in myTemplate
on purpose.