How to prevent display & onCreated + onRendered based on template.instance().data (or other reactive thing)

Hello,

I’d like to display / hide templates based on reactive things, but for all instances of each template types ( which means that any {{#if isReactiveTrue}}{{> myTemplate}}{{/if}} ) won’t work since there are thousands of occurrances of those template calls in all my application.

is there anything like : Template.myTemplate.beforeRender(function(){ return isReactiveTrue; }) ?
Some kind of hooks that display or not the template and that prevent from running onCreated / onRendered when it’s hidden ?

Thanks for your answers :wink:

If I understand correctly, you’d want {{> myTemplate}} to be dynamic, right?

Your best bet is to use Template.dynamic (see http://blazejs.org/api/templates.html#Template-dynamic) and provide the template name and data context from a helper, which is reactive. If the template name is null or undefined, Template.dynamic will not show anything.

I’d like (if possible) to override the {{> }} function so i can check my template datas & name before rendering it or not (depending on something reactive in my connected user attributes)

If not, then i’ll have to change my thousands of {{> }} to Template.dynamic calls, but that’s really my last option :wink:

From the top of my head, I don’t think such thing is possible.

A cleaner way would be, if you’re set to do it right, to use peerlibrary:blaze-components, which is actively maintained. Then re-make the components you want to render conditionally using this pattern: https://github.com/peerlibrary/meteor-blaze-components#programmatic-rendering.

Alternatively, but way more brittle, is to use aldeed:template-extension and replace all those templates with a single one which would be using Template.dynamic and helpers to render, well, the templates whose names you just replaced. Whether this causes any weird side effects, I have no idea. I didn’t use such approach myself.

Good luck!