Skip to content

Commit

Permalink
Parser rewrite
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mjackson committed Jan 11, 2012
1 parent 5df7be5 commit eb01be0
Show file tree
Hide file tree
Showing 72 changed files with 596 additions and 594 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
.DS_Store
.rvmrc
runner.js
lib
jquery.mustache.js
qooxdoo.mustache.js
dojox
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
> What could be more logical awesome than no logic at all?
[mustache.js](http://github.com/janl/mustache.js) is an implementation of the
[Mustache](http://mustache.github.com/) templating system in JavaScript.
[Mustache](http://mustache.github.com/) template system in JavaScript.

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

## Plugins for JavaScript Libraries

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

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

$ rake commonjs
$ rake jquery
$ rake dojo
$ rake yui
Expand Down
9 changes: 1 addition & 8 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ task :spec do
end

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

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

# extra
if opts[:extra]
sh "sed -e 's/{{version}}/#{version}/' #{source}/#{opts[:extra]} \
> #{opts[:location]}/#{opts[:extra]}"
end

puts "Done, see #{opts[:location] || '.'}/#{target_js}"
end
end

templated_build "CommonJS", :location => "lib", :extra => "package.json"
templated_build "jQuery"
templated_build "Dojo", :location => "dojox/string"
templated_build "YUI3", :location => "yui3/mustache"
Expand Down
8 changes: 4 additions & 4 deletions TESTING.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## Running the mustache.js Test Suite
## Running the mustache.js test suite

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

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

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

0 comments on commit eb01be0

Please sign in to comment.