Skip to content

Instantly share code, notes, and snippets.

@ericflo
Created April 11, 2010 20:25
Show Gist options
  • Save ericflo/363036 to your computer and use it in GitHub Desktop.
Save ericflo/363036 to your computer and use it in GitHub Desktop.
<ul>
{{#tweets}}
<li>{{text}}</li>
{{/tweets}}
</ul>
$(document).ready(function() {
var view = {'items': [{"name": "Item1"}, {"name": "Item2"}, {"name": "Item3"}]};
$.mustacheLoad('example.html', view, null, null, function(rendered) {
$('body').html(rendered);
});
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Mustache with AJAX loading support</title>
<script src="jquery-1.4.2.min.js"></script>
<script src="mustache.js"></script>
<script src="jquery.mustache.js"></script>
<script src="example.js"></script>
</head>
<body>
</body>
</html>
(function($){
var templateCache = {};
var mustacheLoad = function(templatePath, view, partials, sendFun, callback) {
var template = templateCache[templatePath];
if(template) {
return callback(Mustache.to_html(template, view, partials, sendFun));
}
$.ajax({
'type': 'GET',
'url': templatePath,
'async': false,
'success': function(data, textStatus) {
templateCache[templatePath] = data;
callback(Mustache.to_html(data, view, partials, sendFun));
}
});
};
$.mustache = Mustache.to_html;
$.mustacheLoad = mustacheLoad;
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment