Project moved here: https://github.com/karolk/temple.js
Created
March 26, 2012 13:53
-
-
Save karolk/2205189 to your computer and use it in GitHub Desktop.
Templating plugin for jQuery working with HTML tree. No <% fancy %> {% brackets %}
This file contains 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
$.fn.temple = function(stash) { | |
var rootNode = this, | |
childNodes = rootNode.find('[data-templ]'), | |
stencil; | |
for (var propName in stash) { | |
if ( $.isArray(stash[propName]) ) { | |
var stashArray = stash[propName]; | |
var $node = childNodes.filter('[data-templ="'+propName+'"]'), | |
isCollectionNode = $node.is('ol, ul, dl, table'); | |
if ($node.length && isCollectionNode) { | |
stencil = $node.children().eq(0); | |
if (stencil.length) { | |
stencil = stencil.clone(); | |
$node.empty(); | |
} | |
stashArray.forEach(function(stashArrayElem) { | |
var $subNode = stencil.clone(); | |
$subNode.temple(stashArrayElem) | |
$node.append($subNode); | |
}) | |
} | |
} | |
else { | |
childNodes | |
.filter('[data-templ="'+propName+'"]') | |
.text(stash[propName]) | |
} | |
} | |
return rootNode; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Live demo:
http://dl.dropbox.com/u/362779/temple.js/index.html