Skip to content

Instantly share code, notes, and snippets.

@mikemellor11
Last active February 11, 2016 15:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikemellor11/9b23a0af217a3c67593c to your computer and use it in GitHub Desktop.
Save mikemellor11/9b23a0af217a3c67593c to your computer and use it in GitHub Desktop.
Handlebars dynamic each loop with object coming from data json
{
"reference": "Content.extraInfo.pdfs"
}
module.exports = function (path, context, option) {
var ret = "";
if(!option){
option = context;
context = option.data.root;
}
context = getProperty(path, context);
for(var i=0, j=context.length; i<j; i++) {
option.data.index = i;
option.data.first = i === 0;
option.data.last = i === (j - 1);
ret = ret + option.fn(context[i]);
}
return ret;
};
function getProperty( propertyName, object ) {
var parts = propertyName.split( "." ),
length = parts.length,
i,
property = object || this;
for ( i = 0; i < length; i++ ) {
property = property[parts[i]];
}
return property;
}
{{#eachDynamic this.reference}}
<li>{{@index}} {{@first}} {{@last}}</li>
{{/eachDynamic}}
// Can override default this context with custom if you like
{{#eachDynamic this.reference @root}}
<li>{{@index}} {{@first}} {{@last}}</li>
{{/eachDynamic}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment