janl/mustache.js — pull request #150 by mjijackson (2011-12-21 21:05:57+00:00)
Title: Compiled templates

I've rewritten the bulk of the mustache.js compiler to support pre-compiled templates. The work resides in the [rewrite branch](https://github.com/janl/mustache.js/tree/rewrite). In order to speed up rendering, templates are pre-compiled to JavaScript functions that take a view and a partials object (and an optional callback) as arguments. There's a jsPerf [here](http://jsperf.com/mustache-js-rewrite/2) that demonstrates the speed difference between master and rewrite in various browsers.

The API exposes two main methods: `Mustache.compile` and `Mustache.render`. The first compiles a template to a reusable JavaScript function and the second renders the template with a view and some partials. `Mustache.to_html` is preserved for backwards compatibility`.

This branch also introduces several other changes:

- It is much more strict about whitespace. In the rewrite, all whitespace that is not on a line with a non-variable tag is preserved. For example, if a line contains only whitespace and a {{#tag}}, that line is ignored. The specs have all been updated to reflect the change, and in several cases (see [here](https://github.com/janl/mustache.js/commit/6443429a7379c59cd09f7a84988c2580d06dd08d#diff-17) for example) I believe they are more consistent with what the user will expect.
- The main mustache.js file is now both browser and CommonJS ready. This was possible with very little extra code, and it supports a very large user base by default without the need for a special wrapper.
- Pragmas are removed, but the {{.}} notation still works for rendering a string when iterating over an array.