Skip to content

Instantly share code, notes, and snippets.

@LulzAugusto
Created July 11, 2014 16:43
Show Gist options
  • Save LulzAugusto/3e0716dda3cf8672c7fe to your computer and use it in GitHub Desktop.
Save LulzAugusto/3e0716dda3cf8672c7fe to your computer and use it in GitHub Desktop.
Template engine with underscore.js
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