Created
July 11, 2014 16:43
-
-
Save LulzAugusto/3e0716dda3cf8672c7fe to your computer and use it in GitHub Desktop.
Template engine with underscore.js
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
function render(tmpl_name, tmpl_data) { | |
if ( !render.tmpl_cache ) { | |
render.tmpl_cache = {}; | |
} | |
if ( ! render.tmpl_cache[tmpl_name] ) { | |
var tmpl_dir = '/templates'; | |
var tmpl_url = tmpl_dir + '/' + tmpl_name + '.html'; | |
var tmpl_string; | |
$.ajax({ | |
url: tmpl_url, | |
method: 'GET', | |
dataType: 'html', | |
async: false, | |
success: function(data) { | |
tmpl_string = data; | |
} | |
}); | |
render.tmpl_cache[tmpl_name] = _.template(tmpl_string); | |
} | |
return render.tmpl_cache[tmpl_name](tmpl_data); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment