Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save staticall/2849538 to your computer and use it in GitHub Desktop.
Save staticall/2849538 to your computer and use it in GitHub Desktop.
Hogan.js local scope and array iteration demo
var template = '<select>{{#sources.countries}}<option value="{{.}}">{{#translate}}country-{{.}}{{/translate}}</option>{{/sources.countries}}</select>';
var template_compiled = Hogan.compile(template);
var data = {
"sources": {
"countries": [
"usa",
"uk",
"france",
"canada",
"germany",
"russia",
"japan",
"china",
"egypt"
]
},
"translate": function(){
return function(text, scope){
text = $.trim(text);
if(typeof scope == 'object')
{
var tpl = Hogan.compile(text);
var res_text = tpl.render(scope);
text = res_text;
}
else
{
// Hook for array iteration
text = text.replace('{{.}}', scope);
}
console.log('Translated element', text);
}
}
}
template_compiled.render(data);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment