Skip to content

Commit

Permalink
render function now fetches templates
Browse files Browse the repository at this point in the history
  • Loading branch information
David Byrd committed May 9, 2013
1 parent 7d39c54 commit dce3866
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions src/ender.js
@@ -1,6 +1,34 @@
var mustache = require('ender-mustache')
, loadTemplate = function (path, callback) { //TODO figure out how to do this without an extra request
$.ajax({
url: path
, type: 'html'
, success: function (res) {
callback(res)
}
})
}



ender.ender({
render: mustache.render,
mustache: mustache
/**
* Either returns rendered HTML or inserts it into a parent element.
*
* @param {string} path on current domain to template.
* @param {Object} data that the template requires.
* @param {string} jQuery style selector for the parent
* element that the rendered template should be inserted into.
*/
render: function (pathToTemplate, data, parentSelector) {
loadTemplate(pathToTemplate, function (template) {
parentSelector ?
Array.prototype.slice.call(document.querySelectorAll(parentSelector)).forEach(function (el){
el.innerHTML = $.render(template, data)
})
: return $.render(template, data)
})

}
, mustache: mustache
})

0 comments on commit dce3866

Please sign in to comment.