Created
June 1, 2012 06:28
-
-
Save staticall/2849538 to your computer and use it in GitHub Desktop.
Hogan.js local scope and array iteration demo
This file contains hidden or 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
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