jsFiddle Example: Loading Mustache Partial Templates Via Ajax
- July 7, 2014 3:49 PM
- Templating, Examples & Tutorials, jQuery, Javascript
- Comments (2)
Mustache is a great, simple, templating language, but the JS implementation (https://github.com/janl/mustache.js) doesn't make it clear that not only can you load partial templates dynamically, you can load them via Ajax (just not asynchronously). This is an example of using a function instead of an object as the partial argument to Mustache's .render() method.
This example has a bit of bit of logic to work around jsFiddle's limitations. For example, in order to let jsFiddle spoof an Ajax call, we need to supply the template data at the time the Ajax call is made, and therefor, the templates must reside in the HTML. In the real world the point of this would be to place each template in its own .html file, and the templateLoader() would be making get requests to load their content directly.
Comments
The value "getTemplate" isn't used in the function though, so saving a reference to it isn't really necessary. You can just change it to a semi-colon and delete the "getTemplate =" part on the next line and it will work fine.