Skip to content

Instantly share code, notes, and snippets.

@katopz
Created July 28, 2014 08:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save katopz/d88ae24e6d903447ef62 to your computer and use it in GitHub Desktop.
Save katopz/d88ae24e6d903447ef62 to your computer and use it in GitHub Desktop.
_.mixin({templateFromUrl: function (url, data, settings) {
var templateHtml = "";
this.cache = this.cache || {};
if (this.cache[url]) {
templateHtml = this.cache[url];
} else {
$.ajax({
url: url,
method: "GET",
async: false,
success: function(data) {
templateHtml = data;
}
});
this.cache[url] = templateHtml;
}
return _.template(templateHtml, data, settings);
}});
// example
var someHtml = _.templateFromUrl("http://example.com/template.html", {"var": "value"});
@TameshwarNirmalkar
Copy link

i used the code and implemented as per my requirement like this.

renderTemplate: function(url, renderid, data){
var compiledTmplate = _.templateFromUrl(url);
$(renderid).html( compiledTmplate( data ) );
}

but when i am using this code. it always return Uncaught ReferenceError: response is not defined whether i am using in ajax success.

like this.

$.ajax({
url: 'getdatafromserver',
type: 'GET',
dataType: 'json',
contentType: "application/json; charset=utf-8",
success: function(res){
var datares = res;
renderTemplate("externaltemplate.html", '#renderid', datares);
}
});

Here it is return var datares = res;
datares undefined..
if you write like this window.datares it doesn't give any error and working fine... but i can't set all the variable in window or global scope.

Please help if you know the solution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment