Skip to content

Instantly share code, notes, and snippets.

@hongymagic
Created May 14, 2012 04:31
Show Gist options
  • Save hongymagic/2691805 to your computer and use it in GitHub Desktop.
Save hongymagic/2691805 to your computer and use it in GitHub Desktop.
Simple client-side only, data-less templating. Dependency on jQuery
// Very simple client-side only, data-less, templating system.
//
// Usage:
// <div data-view="footer" data-type="partial"></div>
// <div data-view="article" data-type="view"></div>
var tempted = function (context) {
if (!context) {
context = window.document;
}
$('[data-view]', context).each(render);
function render (index, view) {
var $view = $(view);
var data = $view.data();
// Partial views have a naming convention of prefixed '_'
var url = 'views/';
if (data.type == 'partial') {
url += '_';
}
url += data.view;
// Fire up AJAX to download data-less HTML and recursively call tempted
var xhr = $.ajax(url, {
success: function (response, status) {
var $html = $(response);
$view.replaceWith($html);
tempted($html);
}
});
}
return this;
};
tempted();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment