Output nested helper assistants that exist in data
I'm getting down to the wall and can't see how I can iterate over the helm assistants that are in the provided data. I have adjusted the jsfiddle to illustrate what I want to do. Any help is appreciated.
You will see that {{position.one}}
in the data is equal to a string Team Lead
, which I can output without problem, but when this helper is used internally {{person.jobTitle}}
, it just outputs Front End {{position.one}}
.
Is there a way to get this output {{position.one}}
that is in {{person.jobTitle}}
so that the output is Front End Team Lead
?
Here is a violin. Thanks for watching!
http://jsfiddle.net/z9u5jz7w/1/
Re: @ luciano-santos Trying to find a way to iterate over everything, regardless of whether the expression is self-contained or the data contains another expression.
I found this library extension ( https://github.com/mateusmaso/handlebars.nested ), but it only seems to work if the expression is found directly in the expressed expression, i.e. {{jobTitle {{position.one}} }}
instead of {{jobTitle}}
actually containing the expression {{position.one}}
.
It looks like I might need to write something to evaluate if the expression contains {{
and reevaluate.
Answer
Ends a custom helper recording for this. Hope this helps someone else too. This helper allows you to evaluate nested expressions found in JSON or your given dataset.
source to share
An expression cannot contain other expressions in descriptors. You'd better prepare your data before submitting to Handlebars like you did it with the name:
var data = {
users: [ {
person: {
firstName: "Garry",
lastName: "Finch"
},
job: {
title: "Front End",
position: "Team Lead"
},
twitter: "gazraa"
} ]
};
This way, you can create a helper, as you did, with the person's name and match both meanings.
Handlebars.registerHelper('fullJob', function(job) {
return job.title + " " + job.position;
});
source to share