Skip to content

Commit eb01be0

Browse files
committed
Parser rewrite
This commit is a complete rewrite of the core mustache.js file with two main goals: 1) a major performance boost and 2) better compliance with the mustache spec. In order to improve performance templates are pre-compiled to JavaScript functions. These compiled functions take a view, a partials object, and an optional callback as arguments. They are cached to prevent unnecessary re-compilation of an already compiled template. Both of these enhancements facilitate a generous boost in performance. A few other notes: - The mustache.js file is now both browser and CommonJS ready without any modification. - The API exposes two main methods: Mustache.compile and Mustache.render. The former is used to generate a function for a given template, while the latter is a higher-level function that is used to compile and render a template in one shot. Mustache.to_html is still available for backwards compatibility. - The concept of pragmas is removed to conform more closely to the original mustache spec. The dot symbol still works to reference the current item in an array. - The parser is much more strict about whitespace than it was before. The rule is simple: if a line contains only a non-variable tag (i.e. not {{tag}} or {{{tag}}}) and whitespace, that line is ignored in the output. Users may use the "space" option when compiling templates to preserve every whitespace character in the original template. - The parser is able to provide detailed information about where errors occur when parsing and rendering templates, including the line number and surrounding code context.
1 parent 5df7be5 commit eb01be0

File tree

72 files changed

+596
-594
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+596
-594
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
.DS_Store
22
.rvmrc
33
runner.js
4-
lib
54
jquery.mustache.js
65
qooxdoo.mustache.js
76
dojox

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
> What could be more logical awesome than no logic at all?
44
55
[mustache.js](http://github.com/janl/mustache.js) is an implementation of the
6-
[Mustache](http://mustache.github.com/) templating system in JavaScript.
6+
[Mustache](http://mustache.github.com/) template system in JavaScript.
77

88
[Mustache](http://mustache.github.com/) is a logic-less template syntax. It can
99
be used for HTML, config files, source code - anything. It works by expanding
@@ -274,10 +274,11 @@ own iteration marker:
274274

275275
## Plugins for JavaScript Libraries
276276

277-
mustache.js may be built specifically for several different client libraries
278-
and platforms, including the following:
277+
By default mustache.js may be used in a browser or any [CommonJS](http://www.commonjs.org/)
278+
environment, including [node](http://nodejs.org/). Additionally, mustache.js may
279+
be built specifically for several different client libraries and platforms,
280+
including the following:
279281

280-
- [node](http://nodejs.org/) (or other CommonJS platforms)
281282
- [jQuery](http://jquery.com/)
282283
- [Dojo](http://www.dojotoolkit.org/)
283284
- [YUI](http://developer.yahoo.com/yui/)
@@ -287,7 +288,6 @@ and platforms, including the following:
287288
These may be built using [Rake](http://rake.rubyforge.org/) and one of the
288289
following commands:
289290

290-
$ rake commonjs
291291
$ rake jquery
292292
$ rake dojo
293293
$ rake yui

Rakefile

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ task :spec do
1313
end
1414

1515
def version
16-
File.read("mustache.js").match('version: "([^\"]+)",$')[1]
16+
File.read("mustache.js").match('version = "([^\"]+)";$')[1]
1717
end
1818

1919
# Creates a rule that uses the .tmpl.{pre,post} stuff to make a final,
@@ -36,17 +36,10 @@ def templated_build(name, opts={})
3636
sh "cat #{source}/#{target_js}.tpl.pre mustache.js \
3737
#{source}/#{target_js}.tpl.post > #{opts[:location] || '.'}/#{target_js}"
3838

39-
# extra
40-
if opts[:extra]
41-
sh "sed -e 's/{{version}}/#{version}/' #{source}/#{opts[:extra]} \
42-
> #{opts[:location]}/#{opts[:extra]}"
43-
end
44-
4539
puts "Done, see #{opts[:location] || '.'}/#{target_js}"
4640
end
4741
end
4842

49-
templated_build "CommonJS", :location => "lib", :extra => "package.json"
5043
templated_build "jQuery"
5144
templated_build "Dojo", :location => "dojox/string"
5245
templated_build "YUI3", :location => "yui3/mustache"

TESTING.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## Running the mustache.js Test Suite
1+
## Running the mustache.js test suite
22

33
The mustache.js test suite uses the [RSpec](http://rspec.info/) testing
44
framework. In order to run the tests you'll need to install [Ruby](http://ruby-lang.org/)
@@ -57,6 +57,6 @@ suite with the following command:
5757

5858
All test files live in the spec/_files directory. To create a new test:
5959

60-
* Create a template file `somename.mustache`
61-
* Create a javascript file with data and functions `somename.js`
62-
* Create a file the expected result `somename.txt`
60+
* Create a template file called `somename.mustache`
61+
* Create a JavaScript file containing the view called `somename.js`
62+
* Create a text file with the expected result called `somename.txt`

0 commit comments

Comments
 (0)