Created
June 1, 2012 06:20
-
-
Save staticall/2849494 to your computer and use it in GitHub Desktop.
Hogan.js local scope 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 = '{{#translate}}user-{{name}}{{/translate}}'; | |
| var template_compiled = Hogan.compile(template); | |
| var data = { | |
| "data": { | |
| "1": {"name": "test"}, | |
| "2": {"name": "example"}, | |
| "3": {"name": "user"} | |
| } | |
| } | |
| $.each(data.data, function(k, val){ | |
| val.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; | |
| } | |
| console.log('Translated element', text); | |
| } | |
| } | |
| template_compiled.render(val); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment