Meteor.js using value of a helper as template - javascript

I would like to render different template based on a helper value. I will try to write and example.
...
{{#with myHelper}}
{{> this }}
{{/with}}
...
with the helper define like this for example:
...
myHelper : function(){
return MyCollection.findOne({ userId: Meteor.userId() }).personalizeTemplate;
}
...
Unfortunately in this way doesn't work. There could be any solution for that?

You need to use a dynamic template You don't even need the {{#with}}
{{> Template.dynamic template=myHelper }}

To use templates dynamically you have to use the global Template.dynamic :
{{> Template.dynamic template="my template"}}
{{> Template.dynamic template=myHelper}}
Note that myHelper must return a String which is the name of the template.
You may also provide a data context with data :
{{> Template.dynamic template=myTemplate data=someData}}
Discover Meteor wrote an article about it. It was created back in 2014 and the UI namespace used throughout the article has since been renamed Template.

Related

Handlebars Partial Data with Dot Notation

In Handlebars, I need to overwrite partial data that data is scoped within a JS object.
The index.hbs file renders multiple partials with different data, but the module properties need to be scoped within the global data object. Overwriting partial attributes using dot notation fails to compile.
Index.hbs
<body>
{{> User }}
{{> User user.name="laura" }} // fails to compile - how to overwrite?
</body>
User.hbs
<div>
Name is: {{name}}
Location is: {{location}}
</div>
Index.js
import index from "Index.hbs";
import user_partial from "User.hbs";
data = {
user: {
name: "kevin",
location: "bar"
}
}
Handlebars.registerPartial(user, user_partial);
document.innerHTML = Handlebars.compile(index)(data);
The documentation says that contexts are passed to the partial like {{> myPartial myOtherContext }} and that additional parameters are passed like {{> myPartial parameter=favoriteNumber }}.
Therefore, it seems you could achieve your objective by passing user as the context and the overrides as parameters. Your template would become:
{{> User user }}
{{> User user name="laura" }}
I have created a fiddle for reference.

Is it possible to pass in a handlebar expression value to a sub-expression?

If my parent context has access to a property (index), can I pass it into a subexpression? I can't seem to get it to work. I've confirmed that that with block works with a static value passed to it (1)
// Parent template
-----------------------------------------------------------------------------
{#child {{../report.ResourceName}}--{{Name}} #data.parentIndex={{#index}} }
// Child template
-----------------------------------------------------------------------------
{{parentIndex}} // displays correctly as 2
// does not render
{{#with (lookup report.sections parentIndex)}}
Rendered: {{name}}
{{/with}}
// renders correctly
{{#with (lookup report.sections 2)}}
Rendered: {{name}}
{{/with}}
when you use dynamic data in a child template call (like an index in a each iteration) you have to be careful because things may not work as your expect.
for example when using something like this
{{#each report.sections}}
{#child child #data.parentIndex={{#index}} }
<br />
{{/each}}
what is passed to the actually child template is "parentIndex": "0 ", it may not be obvious but when you pass data using #data.parentIndex syntax any space matters and since you are using {#child child #data.parentIndex={{#index}} } that contains a space (}) in the end, then the final value contains that space.
the correct way to pass this should be {#child child #data.parentIndex={{#index}}} but this throws a handlebars error because it gets confused by the brackets of the child call syntax.
the way that works is like this:
parent template:
{{#each report.sections}}
{{{callChild #index}}}
<br />
{{/each}}
helpers of parent template:
function callChild(parentIndex) {
return '{#child child #data.parentIndex=' + parentIndex + '}'
}
child template:
{{parentIndex}}
{{#with (lookup report.sections parentIndex)}}
Rendered: {{name}}
{{/with}}
the reason this works is because we are avoiding handlebars getting confused by the brackets syntax, we do that by constructing the child call dynamically with a helper which in the end gets resolved after handlebars have processed the template.
finally here is a live example of this case
hope it helps you.

Meteor data contexts - Global with respect to a single template and its descendants

In Meteor, Imagine a case where:
Session is too global.
Passing data to children templates manually like {{> child specificVar=specificVar}} is too verbose and redundant when you want specificVar to be accessible by every descendant of a template.
How can I get the in-between, where a parent creates a variable, like a ReactiveVar, and any template living within it (children, or their children, etc.) can read and write this variable?
For example:
Javascript
Template.parent.onCreated(function() {
this.specificVar = new ReactiveVar([]);
});
Template.parent.helpers({
parentHelper() {
return Template.instance().specificVar.get();
},
});
Template.child.helpers({
childHelper() {
return Template.instance().specificVar.get();
},
});
Template.grandchild.helpers({
grandchildHelper() {
return Template.instance().specificVar.get();
},
});
HTML
<template name="parent">
{{parentHelper}}
{{> child}}
</template>
<template name="child">
{{childHelper}}
{{> grandchild}}
</template>
<template name="grandchild">
{{grandchildHelper}}
</template>
This kind of data passing isn't directly addressed within vanilla Blaze, but there are a couple workarounds within the Blaze ecosystem.
Template Extensions (https://github.com/aldeed/meteor-template-extension)
This is very similar to what you wanted to do in your example. The templateInstance.get(fieldName) function allows any descendant to fetch a desired property from the ancestor. This allows your grandchild template to write e.g.
Template.grandchild.helpers({
grandchildHelper() {
return Template.instance().get('specificVar').get();
},
});
Blaze Components (https://github.com/peerlibrary/meteor-blaze-components)
This is a more involved solution that involves defining inheritance relationships between parent, child and grandchild templates.
This is more work but also more flexible and robust; The relationships you specify will be independent of DOM structure.

Accessing parent helper in Meteor

I often find myself dividing my work into templates that still could use the same helpers.
So, say I have this template structure:
<template name="MainTemplate">
<div>{{> FirstTemplate}}</div>
<div>{{> SecondTemplate}}</div>
<div>{{> ThirdTemplate}}</div>
<div>{{> FourthTemplate}}</div>
</template>
Now each of these templates wants to use the same helper, let's call it dataHelper:
Template.MainTemplate.helpers({
dataHelper: function() {
//do some stuff
return result
}
})
Sadly, this helper can't be accessed in template first through fourth by simply typing {{dataHelper}} like how events work.
My solution has been to create a global helper instead, but that seems a tad overkill, especially since I have a few pages that don't care about these helpers at all. Another solution is to create four separate helpers but, hey, DRY.
Am I missing something simple here?
There isn't an obvious way to do this in the current version of meteor. One solution is for the child template to "inherit" the helpers from the parent. You can do this pretty easily using meteor-template-extension. Here's an example:
html
<body>
{{> parent}}
</body>
<template name="parent">
<h1>parent</h1>
{{> child}}
</template>
<template name="child">
<h2>child</h2>
<p>{{saySomething}}</p>
</template>
js
Template.parent.helpers({
saySomething: function() {
return Random.choice(['hello', 'dude!', 'i know right?']);
}
});
Template.child.inheritsHelpersFrom('parent');
The template child inherits all of its parent's helpers so it has direct access to saySomething.
This technique has two drawbacks:
you have to specify the inheritsHelpersFrom relationship
all of the parent's helpers are inherited
You can access your parent helpers using either a notation like {{yourParentHelper ..}} with two dots. Have a look here for more informations (end of the article)
You can also access parent data context in javascript like that:
var parent_data = Template.parentData();
By the way, you can add a parameter to reach the third parent, for instance:
var parent_data = Template.parentData(3);
The double dot notation seems to work best within {{#each}} loops, and I'm not having any luck within actual child templates. One option would be to use {{#with}}, although that limits you to basically one helper. e.g.:
<template name="parent">
{{#with dataHelper}}
{{> first}}
{{> second}}
{{/with}}
</template>
This will set the data context of the child helpers to dataHelper, and you can access them with {{this}} inside the template. I suppose you could make dataHelper an object and then pass in multiple pieces of data that way.

Passing variables through handlebars partial

I'm currently dealing with handlebars.js in an express.js application. To keep things modular, I split all my templates in partials.
My problem: I couldn't find a way to pass variables through an partial invocation. Let's say I have a partial which looks like this:
<div id=myPartial>
<h1>Headline<h1>
<p>Lorem ipsum</p>
</div>
Let's assume I registered this partial with the name 'myPartial'. In another template I can then say something like:
<section>
{{> myPartial}}
</section>
This works fine, the partial will be rendered as expected and I'm a happy developer. But what I now need, is a way to pass different variables throught this invocation, to check within a partial for example, if a headline is given or not. Something like:
<div id=myPartial>
{{#if headline}}
<h1>{{headline}}</h1>
{{/if}}
<p>Lorem Ipsum</p>
</div>
And the invokation should look something like this:
<section>
{{> myPartial|'headline':'Headline'}}
</section>
or so.
I know, that I'm able to define all the data I need, before I render a template. But I need a way to do it like just explained. Is there a possible way?
Handlebars partials take a second parameter which becomes the context for the partial:
{{> person this}}
In versions v2.0.0 alpha and later, you can also pass a hash of named parameters:
{{> person headline='Headline'}}
You can see the tests for these scenarios: https://github.com/wycats/handlebars.js/blob/ce74c36118ffed1779889d97e6a2a1028ae61510/spec/qunit_spec.js#L456-L462
https://github.com/wycats/handlebars.js/blob/e290ec24f131f89ddf2c6aeb707a4884d41c3c6d/spec/partials.js#L26-L32
Just in case, here is what I did to get partial arguments, kind of. I’ve created a little helper that takes a partial name and a hash of parameters that will be passed to the partial:
Handlebars.registerHelper('render', function(partialId, options) {
var selector = 'script[type="text/x-handlebars-template"]#' + partialId,
source = $(selector).html(),
html = Handlebars.compile(source)(options.hash);
return new Handlebars.SafeString(html);
});
The key thing here is that Handlebars helpers accept a Ruby-like hash of arguments. In the helper code they come as part of the function’s last argument—options— in its hash member. This way you can receive the first argument—the partial name—and get the data after that.
Then, you probably want to return a Handlebars.SafeString from the helper or use “triple‑stash”—{{{— to prevent it from double escaping.
Here is a more or less complete usage scenario:
<script id="text-field" type="text/x-handlebars-template">
<label for="{{id}}">{{label}}</label>
<input type="text" id="{{id}}"/>
</script>
<script id="checkbox-field" type="text/x-handlebars-template">
<label for="{{id}}">{{label}}</label>
<input type="checkbox" id="{{id}}"/>
</script>
<script id="form-template" type="text/x-handlebars-template">
<form>
<h1>{{title}}</h1>
{{ render 'text-field' label="First name" id="author-first-name" }}
{{ render 'text-field' label="Last name" id="author-last-name" }}
{{ render 'text-field' label="Email" id="author-email" }}
{{ render 'checkbox-field' label="Private?" id="private-question" }}
</form>
</script>
Hope this helps …someone. :)
This can also be done in later versions of handlebars using the key=value notation:
{{> mypartial foo='bar' }}
Allowing you to pass specific values to your partial context.
Reference: Context different for partial #182
This is very possible if you write your own helper. We are using a custom $ helper to accomplish this type of interaction (and more):
/*///////////////////////
Adds support for passing arguments to partials. Arguments are merged with
the context for rendering only (non destructive). Use `:token` syntax to
replace parts of the template path. Tokens are replace in order.
USAGE: {{$ 'path.to.partial' context=newContext foo='bar' }}
USAGE: {{$ 'path.:1.:2' replaceOne replaceTwo foo='bar' }}
///////////////////////////////*/
Handlebars.registerHelper('$', function(partial) {
var values, opts, done, value, context;
if (!partial) {
console.error('No partial name given.');
}
values = Array.prototype.slice.call(arguments, 1);
opts = values.pop();
while (!done) {
value = values.pop();
if (value) {
partial = partial.replace(/:[^\.]+/, value);
}
else {
done = true;
}
}
partial = Handlebars.partials[partial];
if (!partial) {
return '';
}
context = _.extend({}, opts.context||this, _.omit(opts, 'context', 'fn', 'inverse'));
return new Handlebars.SafeString( partial(context) );
});
Sounds like you want to do something like this:
{{> person {another: 'attribute'} }}
Yehuda already gave you a way of doing that:
{{> person this}}
But to clarify:
To give your partial its own data, just give it its own model inside the existing model, like so:
{{> person this.childContext}}
In other words, if this is the model you're giving to your template:
var model = {
some : 'attribute'
}
Then add a new object to be given to the partial:
var model = {
some : 'attribute',
childContext : {
'another' : 'attribute' // this goes to the child partial
}
}
childContext becomes the context of the partial like Yehuda said -- in that, it only sees the field another, but it doesn't see (or care about the field some). If you had id in the top level model, and repeat id again in the childContext, that'll work just fine as the partial only sees what's inside childContext.
The accepted answer works great if you just want to use a different context in your partial. However, it doesn't let you reference any of the parent context. To pass in multiple arguments, you need to write your own helper. Here's a working helper for Handlebars 2.0.0 (the other answer works for versions <2.0.0):
Handlebars.registerHelper('renderPartial', function(partialName, options) {
if (!partialName) {
console.error('No partial name given.');
return '';
}
var partial = Handlebars.partials[partialName];
if (!partial) {
console.error('Couldnt find the compiled partial: ' + partialName);
return '';
}
return new Handlebars.SafeString( partial(options.hash) );
});
Then in your template, you can do something like:
{{renderPartial 'myPartialName' foo=this bar=../bar}}
And in your partial, you'll be able to access those values as context like:
<div id={{bar.id}}>{{foo}}</div>
Yes, I was late, but I can add for Assemble users: you can use buil-in "parseJSON" helper http://assemble.io/helpers/helpers-data.html. (Discovered in https://github.com/assemble/assemble/issues/416).
Not sure if this is helpful but here's an example of Handlebars template with dynamic parameters passed to an inline RadioButtons partial and the client(browser) rendering the radio buttons in the container.
For my use it's rendered with Handlebars on the server and lets the client finish it up.
With it a forms tool can provide inline data within Handlebars without helpers.
Note : This example requires jQuery
{{#*inline "RadioButtons"}}
{{name}} Buttons<hr>
<div id="key-{{{name}}}"></div>
<script>
{{{buttons}}}.map((o)=>{
$("#key-{{name}}").append($(''
+'<button class="checkbox">'
+'<input name="{{{name}}}" type="radio" value="'+o.value+'" />'+o.text
+'</button>'
));
});
// A little test script
$("#key-{{{name}}} .checkbox").on("click",function(){
alert($("input",this).val());
});
</script>
{{/inline}}
{{>RadioButtons name="Radio" buttons='[
{value:1,text:"One"},
{value:2,text:"Two"},
{value:3,text:"Three"}]'
}}

Categories