Created
May 14, 2012 04:31
-
-
Save hongymagic/2691805 to your computer and use it in GitHub Desktop.
Simple client-side only, data-less templating. Dependency on jQuery
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
// 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