Difference between compile(), parse(), and render() in mustache.js

alnafie picture alnafie · Feb 21, 2012 · Viewed 11.1k times · Source

What is the difference between:

Mustache.compile() , Mustache.parse(), and Mustache.render()

in the new mustache.js version 0.5.0, and perhaps for bonus points you could tell us what the difference between parsing and compiling is in general.

Answer

Tomalak picture Tomalak · Feb 21, 2012

EDIT

With an API change introduced in version 0.8.0, the compile() method has been integrated into parse(). Manually compiling the templates is no longer required.


Mustache.parse()

Syntactically parses the template and creates a JavaScript function body (a string) from it. During that process it notifies of any syntax errors encountered in the template.

Mustache.compile()

Uses the function body returned from a successful parse() to create an actual JavaScript function. The created function is placed in a cache for re-use.

Mustache.render()

Takes the appropriate function for a given template (the one that was created by compile()) and applies it to actual data. This creates the result meant to be shown on screen.