最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Difference between compile(), parse(), and render() in mustache.js - Stack Overflow

programmeradmin0浏览0评论

What is the difference between:

Mustachepile() , 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.

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.

Share Improve this question asked Feb 21, 2012 at 20:27 alnafiealnafie 10.8k7 gold badges29 silver badges46 bronze badges 1
  • 15 Read the source, I have, but still, see the difference, I cannot. – alnafie Commented Feb 21, 2012 at 20:40
Add a comment  | 

3 Answers 3

Reset to default 20

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.

Just a tip Mustache.parse(template) is optional and speeds up future uses of template. This is useful when you want to reuse your template with a set of (large) data. If this is not the case a call to the Mustache.render(), which generates the final result, is enough.

A little extra: If you working with custom delimiters (instead of {{ and }}), you can use Mustache.parse before calling the Mustache.render with the custom delimiters as a parameter.

Example:

var template = "<h1>Hello {{$name$}}</h1>"
var customTags = [ '{{$', '$}}' ];
Mustache.parse(template, cutomTags);
var result = Mustache.render(template, { name: "John" };
console.log(result);

Will result In:

Hello John

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论