Last active
February 11, 2016 15:52
-
-
Save mikemellor11/9b23a0af217a3c67593c to your computer and use it in GitHub Desktop.
Handlebars dynamic each loop with object coming from data json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"reference": "Content.extraInfo.pdfs" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment