Skip to content

Instantly share code, notes, and snippets.

@AlyxRen
Created October 15, 2010 17:01
Show Gist options
  • Save AlyxRen/628546 to your computer and use it in GitHub Desktop.
Save AlyxRen/628546 to your computer and use it in GitHub Desktop.
Load a template file from a external source on the same domain. supports passing AJAX data for dynamic templates, as well as caching.
(function($){
var cache, defaults;
cache = {};
defaults = {
//ajax information
'url': "",
'method': "GET",
'ajxData': {},
//template Options
'cache': true,
'execute': false,
'data': false,
'callBack': false
};
$.extend({
'extTmpl': function(options){
var opt,results;
opt = $.extend({},defaults,options);
if ( opt.cache && cache[opt.url] + JSON.stringify( opt.ajxData ) ){
results = $.tmpl( cache[opt.url], opt.data )
calback && callback.call(
results,
results
);
}else {
$.ajax({
url: opt.url,
type: opt.method,
data: opt.ajxData,
success: function(data,success,xhr){
vartmpl, results;
tmpl = div
.clone()
.text(data)
.template();
if (opt.cache){ cache[
opt.url + JSON.stringify( opt.ajxData )
] = tmpl; }
opt.execute && ( results = $.tmpl( tmpl, opt.data ) );
callback && callback.call(
results, //this
results
);
}
});
}
}
});
}(jQuery));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment