I am writing a special Blaze block helper with children:
<template name="parent"> {{> Template.contentBlock ..}} </template> <template name="child"> {{> Template.contentBlock ..}} </template>
The supposed use case should be to have a template with arbitrary child nodes, which I define in an html file.
{{#parent}} {{#child id="child1" title="Child 1"}} <p>This is content of child 1</p> {{/child}} {{#child id="child2" title="Child 2"}} <p>This is content of child 2</p> {{/child}} {{#child id="childN" title="Child N"}} <p>This is content of child N</p> {{/child}} {{/parent}}
There are no problems so far. However, in the parent onCreated / autorun template, I want to have access to the child templates. I want to use this data to dynamically create in parent elements of a template based on
Template.parent.onCreated(function () { const instance = this; instance.state = new ReactiveDict(); instance.autorun(function () { const contentBlocks =
Where children will be used in the parent template as follows:
{{#parent}} {{#each children}} do something with {{this.value}} {{/each}} {{#child id="child1" title="Child 1"}} <p>This is content of child 1</p> {{/child}} {{#child id="child2" title="Child 2"}} <p>This is content of child 2</p> {{/child}} {{#child id="childN" title="Child N"}} <p>This is content of child N</p> {{/child}} {{/parent}}
I do not want to access the contents of contentBlock ( <p> ), but rather to get a list of child templates added.
Is this possible with the current template API / Blaze? The documentation is a bit thin at this point.
This is basically the opposite of this post: How to get the parent template instance (of the current template)
Edit 1: Use parent view Renderfunction (only partially works)
I found a way to return the parent Template to the children, but not their data :
Another approach I found is to use a generic ReactiveVar , but both seem to me not clean enough. I just want to get a list of Template instances in the parent js code.
Edit 2: use generic ReactiveVar (only partially working)
You can use the generic ReactiveVar if it is in the scope of both patterns:
const _cache = new ReactiveVar({}); Template.parent.onCreated(function () { const instance = this; instance.state = new ReactiveDict(); instance.autorun(function () { const children = Object.values(_cache.get()); instance.state.set("children", children); }); }); Template.parent.helpers({ children() { return Template.instance().state.get("children"); } });
It works (but is displayed only once, but does not respond):
Template.child.onCreated(function () { const instance = this; const data = Template.currentData(); const cache = _cache.get(); cache[data.id] = data; _cache.set(cache); });
Does not work (child autostart sets values, but new values ββare not displayed):
Template.child.onCreated(function () { const instance = this; instance.autorun(function() { const instance = this; const data = Template.currentData(); const cache = _cache.get(); cache[data.id] = data; _cache.set(cache); }); });