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

What's 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 is between parsing and compilation in general.

+8
javascript templates mustache
source share
2 answers

EDIT

With the API change introduced in version 0.8.0 , the compile() method was integrated into parse() . Manual compilation of templates is no longer required.


Mustache.parse()

It parses the template and creates the body of the JavaScript function (string) from it. During this process, it notifies of any syntax errors encountered in the template.

Mustache.compile()

Uses the function body returned from successful parse() to create the actual JavaScript function. The created function is cached for reuse.

Mustache.render()

It takes the appropriate function for the given template (the one compile() ) and applies it to the actual data. This creates a result that should be displayed on the screen.

+15
source share

Only the Mustache.parse(template) hint is optional and speeds up the use of templates in the future. This is useful if you want to reuse your template with a set of (big) data. If this is not the case, then calling Mustache.render() , which generates the final result, is sufficient.

+1
source share

All Articles