{{street}}
-{{city}}, {{state}} {{zip}}
- {{/address}} +Output: -We'll get this output: + Moe + Larry + Curly -801 Streetly street
-Boston, MA 02101
+When looping over an array of strings, a `.` can be used to refer to the current +item in the list. -### Inverted Sections +Template: -An inverted section opens with `{{^section}}` instead of `{{#section}}` and -uses a boolean negative to evaluate. Empty arrays are considered falsy. + {{#musketeers}} + * {{.}} + {{/musketeers}} View: - var inverted_section = { - "repo": [] + { + "musketeers": ["Athos", "Aramis", "Porthos", "D'Artagnan"] } -Template: - - {{#repo}}{{name}}{{/repo}} - {{^repo}}No repos :({{/repo}} +Output: -Result: + * Athos + * Aramis + * Porthos + * D'Artagnan - No repos :( +If the value of a section variable is a function, it will be called in the +context of the current item in the list on each iteration. +Template: -### View Partials + {{#beatles}} + * {{name}} + {{/beatles}} -mustache.js supports a quite powerful but yet simple view partial mechanism. -Use the following syntax for partials: `{{>partial_name}}` +View: - var view = { - name: "Joe", - winnings: { - value: 1000, - taxed_value: function() { - return this.value - (this.value * 0.4); - } + { + "beatles": [ + { "firstName": "John", "lastName": "Lennon" }, + { "firstName": "Paul", "lastName": "McCartney" }, + { "firstName": "George", "lastName": "Harrison" }, + { "firstName": "Ringo", "lastName": "Starr" } + ], + "name": function () { + return this.firstName + " " + this.lastName; } - }; - - var template = "Welcome, {{name}}! {{>winnings}}" - var partials = { - winnings: "You just won ${{value}} (which is ${{taxed_value}} after tax)"}; - - var output = Mustache.to_html(template, view, partials) - - output will be: - Welcome, Joe! You just won $1000 (which is $600 after tax) - -You invoke a partial with `{{>winnings}}`. Invoking the partial `winnings` -will tell mustache.js to look for a object in the context's property -`winnings`. It will then use that object as the context for the template found -in `partials` for `winnings`. - -## Escaping - -mustache.js does escape all values when using the standard double mustache -syntax. Characters which will be escaped: `& \ " ' < >`. To disable escaping, -simply use triple mustaches like `{{{unescaped_variable}}}`. - -Example: Using `{{variable}}` inside a template for `5 > 2` will result in `5 > 2`, where as the usage of `{{{variable}}}` will result in `5 > 2`. + } +Output: -## Streaming + * John Lennon + * Paul McCartney + * George Harrison + * Ringo Starr -To stream template results out of mustache.js, you can pass an optional -`send()` callback to the `to_html()` call: +#### Functions - Mustache.to_html(template, view, partials, function(line) { - print(line); - }); +If the value of a section key is a function, it is called with the section's +literal block of text, un-rendered, as its first argument. The second argument +is a special rendering function that uses the current view as its view argument. +It is called in the context of the current view object. +Template: -## Pragmas + {{#bold}}Hi {{name}}.{{/bold}} -Pragma tags let you alter the behaviour of mustache.js. They have the format -of +View: - {{%PRAGMANAME}} + { + "name": "Tater", + "bold": function () { + return function (text, render) { + return "" + render(text) + ""; + } + } + } -and they accept options: +Output: - {{%PRAGMANAME option=value}} + Hi Tater. +### Inverted Sections -### IMPLICIT-ITERATOR +An inverted section opens with `{{^section}}` instead of `{{#section}}`. The +block of an inverted section is rendered only if the value of that section's tag +is `null`, `undefined`, `false`, or an empty list. -When using a block to iterate over an enumerable (Array), mustache.js expects -an objects as enumerable items. The implicit iterator pragma enables optional -behaviour of allowing literals as enumerable items. Consider this view: +Template: - var view = { - foo: [1, 2, 3, 4, 5, "french"] - }; + {{#repos}}{{name}}{{/repos}} + {{^repos}}No repos :({{/repos}} -The following template can iterate over the member `foo`: +View: - {{%IMPLICIT-ITERATOR}} - {{#foo}} - {{.}} - {{/foo}} + { + "repos": [] + } -If you don't like the dot in there, the pragma accepts an option to set your -own iteration marker: +Output: - {{%IMPLICIT-ITERATOR iterator=bob}} - {{#foo}} - {{bob}} - {{/foo}} + No repos :( -## More Examples and Documentation +### Comments -See `examples/` for more goodies and read the [original mustache docs][m] +Comments begin with a bang and are ignored. The following template: -## Command Line +"
+};
diff --git a/spec/_files/ampersand_escape.mustache b/spec/_files/ampersand_escape.mustache
new file mode 100644
index 0000000..6501a48
--- /dev/null
+++ b/spec/_files/ampersand_escape.mustache
@@ -0,0 +1 @@
+{{&message}}
diff --git a/spec/_files/ampersand_escape.txt b/spec/_files/ampersand_escape.txt
new file mode 100644
index 0000000..2ed3fd3
--- /dev/null
+++ b/spec/_files/ampersand_escape.txt
@@ -0,0 +1 @@
+Some
diff --git a/examples/apostrophe.js b/spec/_files/apostrophe.js
similarity index 100%
rename from examples/apostrophe.js
rename to spec/_files/apostrophe.js
diff --git a/examples/apostrophe.html b/spec/_files/apostrophe.mustache
similarity index 100%
rename from examples/apostrophe.html
rename to spec/_files/apostrophe.mustache
diff --git a/examples/apostrophe.txt b/spec/_files/apostrophe.txt
similarity index 100%
rename from examples/apostrophe.txt
rename to spec/_files/apostrophe.txt
diff --git a/examples/array_of_strings.js b/spec/_files/array_of_strings.js
similarity index 100%
rename from examples/array_of_strings.js
rename to spec/_files/array_of_strings.js
diff --git a/spec/_files/array_of_strings.mustache b/spec/_files/array_of_strings.mustache
new file mode 100644
index 0000000..4d65738
--- /dev/null
+++ b/spec/_files/array_of_strings.mustache
@@ -0,0 +1 @@
+{{#array_of_strings}}{{.}} {{/array_of_strings}}
diff --git a/examples/array_of_strings.txt b/spec/_files/array_of_strings.txt
similarity index 100%
rename from examples/array_of_strings.txt
rename to spec/_files/array_of_strings.txt
diff --git a/spec/_files/backslashes.js b/spec/_files/backslashes.js
new file mode 100644
index 0000000..0b9765b
--- /dev/null
+++ b/spec/_files/backslashes.js
@@ -0,0 +1,3 @@
+var backslashes = {
+ value: "\\abc"
+};
diff --git a/spec/_files/backslashes.mustache b/spec/_files/backslashes.mustache
new file mode 100644
index 0000000..fe7745b
--- /dev/null
+++ b/spec/_files/backslashes.mustache
@@ -0,0 +1,7 @@
+* {{value}}
+* {{{value}}}
+* {{&value}}
+
diff --git a/spec/_files/backslashes.txt b/spec/_files/backslashes.txt
new file mode 100644
index 0000000..038dd37
--- /dev/null
+++ b/spec/_files/backslashes.txt
@@ -0,0 +1,7 @@
+* \abc
+* \abc
+* \abc
+
diff --git a/examples/bug_11_eating_whitespace.js b/spec/_files/bug_11_eating_whitespace.js
similarity index 100%
rename from examples/bug_11_eating_whitespace.js
rename to spec/_files/bug_11_eating_whitespace.js
diff --git a/examples/bug_11_eating_whitespace.html b/spec/_files/bug_11_eating_whitespace.mustache
similarity index 100%
rename from examples/bug_11_eating_whitespace.html
rename to spec/_files/bug_11_eating_whitespace.mustache
diff --git a/examples/bug_11_eating_whitespace.txt b/spec/_files/bug_11_eating_whitespace.txt
similarity index 100%
rename from examples/bug_11_eating_whitespace.txt
rename to spec/_files/bug_11_eating_whitespace.txt
diff --git a/spec/_files/changing_delimiters.js b/spec/_files/changing_delimiters.js
new file mode 100644
index 0000000..4fbe9f3
--- /dev/null
+++ b/spec/_files/changing_delimiters.js
@@ -0,0 +1,4 @@
+var changing_delimiters = {
+ "foo": "foooooooooooooo",
+ "bar":"bar!"
+};
diff --git a/spec/_files/changing_delimiters.mustache b/spec/_files/changing_delimiters.mustache
new file mode 100644
index 0000000..0cd044c
--- /dev/null
+++ b/spec/_files/changing_delimiters.mustache
@@ -0,0 +1 @@
+{{=<% %>=}}<% foo %> {{foo}} <%{bar}%> {{{bar}}}
diff --git a/spec/_files/changing_delimiters.txt b/spec/_files/changing_delimiters.txt
new file mode 100644
index 0000000..1b1510d
--- /dev/null
+++ b/spec/_files/changing_delimiters.txt
@@ -0,0 +1 @@
+foooooooooooooo {{foo}} bar! {{{bar}}}
diff --git a/examples/comments.js b/spec/_files/comments.js
similarity index 100%
rename from examples/comments.js
rename to spec/_files/comments.js
diff --git a/examples/comments.html b/spec/_files/comments.mustache
similarity index 100%
rename from examples/comments.html
rename to spec/_files/comments.mustache
diff --git a/examples/comments.txt b/spec/_files/comments.txt
similarity index 100%
rename from examples/comments.txt
rename to spec/_files/comments.txt
diff --git a/examples/complex.js b/spec/_files/complex.js
similarity index 100%
rename from examples/complex.js
rename to spec/_files/complex.js
diff --git a/examples/complex.html b/spec/_files/complex.mustache
similarity index 95%
rename from examples/complex.html
rename to spec/_files/complex.mustache
index 23bec3c..869a4f0 100644
--- a/examples/complex.html
+++ b/spec/_files/complex.mustache
@@ -13,4 +13,4 @@
{{/list}}
{{#empty}}
The list is empty.
-{{/empty}}
\ No newline at end of file
+{{/empty}}
diff --git a/spec/_files/complex.txt b/spec/_files/complex.txt
new file mode 100644
index 0000000..596d3f6
--- /dev/null
+++ b/spec/_files/complex.txt
@@ -0,0 +1,6 @@
+Colors
+
diff --git a/spec/_files/context_lookup.js b/spec/_files/context_lookup.js
new file mode 100644
index 0000000..49d4453
--- /dev/null
+++ b/spec/_files/context_lookup.js
@@ -0,0 +1,8 @@
+var context_lookup = {
+ "outer": {
+ "id": 1,
+ "second": {
+ "nothing": 2
+ }
+ }
+};
diff --git a/spec/_files/context_lookup.mustache b/spec/_files/context_lookup.mustache
new file mode 100644
index 0000000..3c7b767
--- /dev/null
+++ b/spec/_files/context_lookup.mustache
@@ -0,0 +1 @@
+{{#outer}}{{#second}}{{id}}{{/second}}{{/outer}}
diff --git a/spec/_files/context_lookup.txt b/spec/_files/context_lookup.txt
new file mode 100644
index 0000000..d00491f
--- /dev/null
+++ b/spec/_files/context_lookup.txt
@@ -0,0 +1 @@
+1
diff --git a/examples/delimiters.js b/spec/_files/delimiters.js
similarity index 100%
rename from examples/delimiters.js
rename to spec/_files/delimiters.js
diff --git a/examples/delimiters.html b/spec/_files/delimiters.mustache
similarity index 82%
rename from examples/delimiters.html
rename to spec/_files/delimiters.mustache
index a73e061..7fac846 100644
--- a/examples/delimiters.html
+++ b/spec/_files/delimiters.mustache
@@ -4,4 +4,4 @@
<%=| |=%>
* | third |
|={{ }}=|
-* {{ fourth }}
\ No newline at end of file
+* {{ fourth }}
diff --git a/examples/delimiters.txt b/spec/_files/delimiters.txt
similarity index 100%
rename from examples/delimiters.txt
rename to spec/_files/delimiters.txt
diff --git a/spec/_files/disappearing_whitespace.js b/spec/_files/disappearing_whitespace.js
new file mode 100644
index 0000000..3fe7121
--- /dev/null
+++ b/spec/_files/disappearing_whitespace.js
@@ -0,0 +1,4 @@
+var disappearing_whitespace = {
+ bedrooms: true,
+ total: 1
+};
diff --git a/spec/_files/disappearing_whitespace.mustache b/spec/_files/disappearing_whitespace.mustache
new file mode 100644
index 0000000..16c16e0
--- /dev/null
+++ b/spec/_files/disappearing_whitespace.mustache
@@ -0,0 +1 @@
+{{#bedrooms}}{{total}}{{/bedrooms}} BED
diff --git a/spec/_files/disappearing_whitespace.txt b/spec/_files/disappearing_whitespace.txt
new file mode 100644
index 0000000..66e98ef
--- /dev/null
+++ b/spec/_files/disappearing_whitespace.txt
@@ -0,0 +1 @@
+1 BED
diff --git a/examples/dot_notation.js b/spec/_files/dot_notation.js
similarity index 100%
rename from examples/dot_notation.js
rename to spec/_files/dot_notation.js
diff --git a/examples/dot_notation.html b/spec/_files/dot_notation.mustache
similarity index 100%
rename from examples/dot_notation.html
rename to spec/_files/dot_notation.mustache
diff --git a/examples/dot_notation.txt b/spec/_files/dot_notation.txt
similarity index 100%
rename from examples/dot_notation.txt
rename to spec/_files/dot_notation.txt
diff --git a/examples/double_render.js b/spec/_files/double_render.js
similarity index 100%
rename from examples/double_render.js
rename to spec/_files/double_render.js
diff --git a/examples/double_render.html b/spec/_files/double_render.mustache
similarity index 100%
rename from examples/double_render.html
rename to spec/_files/double_render.mustache
diff --git a/examples/double_render.txt b/spec/_files/double_render.txt
similarity index 100%
rename from examples/double_render.txt
rename to spec/_files/double_render.txt
diff --git a/spec/_files/empty_list.js b/spec/_files/empty_list.js
new file mode 100644
index 0000000..80555c0
--- /dev/null
+++ b/spec/_files/empty_list.js
@@ -0,0 +1,3 @@
+var empty_list = {
+ jobs: []
+};
diff --git a/spec/_files/empty_list.mustache b/spec/_files/empty_list.mustache
new file mode 100644
index 0000000..4fdf13d
--- /dev/null
+++ b/spec/_files/empty_list.mustache
@@ -0,0 +1,4 @@
+These are the jobs:
+{{#jobs}}
+{{.}}
+{{/jobs}}
diff --git a/spec/_files/empty_list.txt b/spec/_files/empty_list.txt
new file mode 100644
index 0000000..d9b4a67
--- /dev/null
+++ b/spec/_files/empty_list.txt
@@ -0,0 +1 @@
+These are the jobs:
diff --git a/examples/empty_sections.js b/spec/_files/empty_sections.js
similarity index 100%
rename from examples/empty_sections.js
rename to spec/_files/empty_sections.js
diff --git a/examples/empty_sections.html b/spec/_files/empty_sections.mustache
similarity index 100%
rename from examples/empty_sections.html
rename to spec/_files/empty_sections.mustache
diff --git a/examples/empty_sections.txt b/spec/_files/empty_sections.txt
similarity index 100%
rename from examples/empty_sections.txt
rename to spec/_files/empty_sections.txt
diff --git a/spec/_files/empty_string.js b/spec/_files/empty_string.js
new file mode 100644
index 0000000..2151336
--- /dev/null
+++ b/spec/_files/empty_string.js
@@ -0,0 +1,6 @@
+var empty_string = {
+ description: "That is all!",
+ child: {
+ description: ""
+ }
+};
diff --git a/spec/_files/empty_string.mustache b/spec/_files/empty_string.mustache
new file mode 100644
index 0000000..f568441
--- /dev/null
+++ b/spec/_files/empty_string.mustache
@@ -0,0 +1 @@
+{{description}}{{#child}}{{description}}{{/child}}
diff --git a/spec/_files/empty_string.txt b/spec/_files/empty_string.txt
new file mode 100644
index 0000000..22e2a6e
--- /dev/null
+++ b/spec/_files/empty_string.txt
@@ -0,0 +1 @@
+That is all!
diff --git a/examples/empty_template.js b/spec/_files/empty_template.js
similarity index 100%
rename from examples/empty_template.js
rename to spec/_files/empty_template.js
diff --git a/examples/empty_template.html b/spec/_files/empty_template.mustache
similarity index 100%
rename from examples/empty_template.html
rename to spec/_files/empty_template.mustache
diff --git a/spec/_files/empty_template.txt b/spec/_files/empty_template.txt
new file mode 100644
index 0000000..bb2367a
--- /dev/null
+++ b/spec/_files/empty_template.txt
@@ -0,0 +1 @@
+Test
\ No newline at end of file
diff --git a/examples/error_not_found.js b/spec/_files/error_not_found.js
similarity index 100%
rename from examples/error_not_found.js
rename to spec/_files/error_not_found.js
diff --git a/examples/error_not_found.html b/spec/_files/error_not_found.mustache
similarity index 100%
rename from examples/error_not_found.html
rename to spec/_files/error_not_found.mustache
diff --git a/spec/_files/error_not_found.txt b/spec/_files/error_not_found.txt
new file mode 100644
index 0000000..e69de29
diff --git a/examples/escaped.js b/spec/_files/escaped.js
similarity index 100%
rename from examples/escaped.js
rename to spec/_files/escaped.js
diff --git a/examples/escaped.html b/spec/_files/escaped.mustache
similarity index 100%
rename from examples/escaped.html
rename to spec/_files/escaped.mustache
diff --git a/examples/escaped.txt b/spec/_files/escaped.txt
similarity index 100%
rename from examples/escaped.txt
rename to spec/_files/escaped.txt
diff --git a/examples/higher_order_sections.js b/spec/_files/higher_order_sections.js
similarity index 100%
rename from examples/higher_order_sections.js
rename to spec/_files/higher_order_sections.js
diff --git a/examples/higher_order_sections.html b/spec/_files/higher_order_sections.mustache
similarity index 100%
rename from examples/higher_order_sections.html
rename to spec/_files/higher_order_sections.mustache
diff --git a/examples/higher_order_sections.txt b/spec/_files/higher_order_sections.txt
similarity index 100%
rename from examples/higher_order_sections.txt
rename to spec/_files/higher_order_sections.txt
diff --git a/spec/_files/included_tag.js b/spec/_files/included_tag.js
new file mode 100644
index 0000000..a63df27
--- /dev/null
+++ b/spec/_files/included_tag.js
@@ -0,0 +1,3 @@
+var included_tag = {
+ html: "I like {{mustache}}"
+};
diff --git a/spec/_files/included_tag.mustache b/spec/_files/included_tag.mustache
new file mode 100644
index 0000000..70631c2
--- /dev/null
+++ b/spec/_files/included_tag.mustache
@@ -0,0 +1 @@
+You said "{{{html}}}" today
diff --git a/spec/_files/included_tag.txt b/spec/_files/included_tag.txt
new file mode 100644
index 0000000..1af4556
--- /dev/null
+++ b/spec/_files/included_tag.txt
@@ -0,0 +1 @@
+You said "I like {{mustache}}" today
diff --git a/examples/inverted_section.js b/spec/_files/inverted_section.js
similarity index 60%
rename from examples/inverted_section.js
rename to spec/_files/inverted_section.js
index cb96ecf..2e07fc3 100644
--- a/examples/inverted_section.js
+++ b/spec/_files/inverted_section.js
@@ -1,3 +1,3 @@
var inverted_section = {
- "repo": []
-}
+ "repos": []
+};
diff --git a/spec/_files/inverted_section.mustache b/spec/_files/inverted_section.mustache
new file mode 100644
index 0000000..b0a183b
--- /dev/null
+++ b/spec/_files/inverted_section.mustache
@@ -0,0 +1,3 @@
+{{#repos}}{{name}}{{/repos}}
+{{^repos}}No repos :({{/repos}}
+{{^nothin}}Hello!{{/nothin}}
diff --git a/examples/inverted_section.txt b/spec/_files/inverted_section.txt
similarity index 60%
rename from examples/inverted_section.txt
rename to spec/_files/inverted_section.txt
index 5fd0de1..b421582 100644
--- a/examples/inverted_section.txt
+++ b/spec/_files/inverted_section.txt
@@ -1 +1,3 @@
+
No repos :(
+Hello!
diff --git a/examples/keys_with_questionmarks.js b/spec/_files/keys_with_questionmarks.js
similarity index 100%
rename from examples/keys_with_questionmarks.js
rename to spec/_files/keys_with_questionmarks.js
diff --git a/examples/keys_with_questionmarks.html b/spec/_files/keys_with_questionmarks.mustache
similarity index 100%
rename from examples/keys_with_questionmarks.html
rename to spec/_files/keys_with_questionmarks.mustache
diff --git a/examples/keys_with_questionmarks.txt b/spec/_files/keys_with_questionmarks.txt
similarity index 100%
rename from examples/keys_with_questionmarks.txt
rename to spec/_files/keys_with_questionmarks.txt
diff --git a/spec/_files/multiline_comment.js b/spec/_files/multiline_comment.js
new file mode 100644
index 0000000..24e7445
--- /dev/null
+++ b/spec/_files/multiline_comment.js
@@ -0,0 +1 @@
+var multiline_comment = {};
diff --git a/spec/_files/multiline_comment.mustache b/spec/_files/multiline_comment.mustache
new file mode 100644
index 0000000..dff0893
--- /dev/null
+++ b/spec/_files/multiline_comment.mustache
@@ -0,0 +1,6 @@
+{{!
+
+This is a multi-line comment.
+
+}}
+Hello world!
diff --git a/spec/_files/multiline_comment.txt b/spec/_files/multiline_comment.txt
new file mode 100644
index 0000000..cd08755
--- /dev/null
+++ b/spec/_files/multiline_comment.txt
@@ -0,0 +1 @@
+Hello world!
diff --git a/spec/_files/nested_iterating.js b/spec/_files/nested_iterating.js
new file mode 100644
index 0000000..a8275f9
--- /dev/null
+++ b/spec/_files/nested_iterating.js
@@ -0,0 +1,8 @@
+var nested_iterating = {
+ inner: [{
+ foo: 'foo',
+ inner: [{
+ bar: 'bar'
+ }]
+ }]
+};
diff --git a/spec/_files/nested_iterating.mustache b/spec/_files/nested_iterating.mustache
new file mode 100644
index 0000000..1a3bb1a
--- /dev/null
+++ b/spec/_files/nested_iterating.mustache
@@ -0,0 +1 @@
+{{#inner}}{{foo}}{{#inner}}{{bar}}{{/inner}}{{/inner}}
diff --git a/spec/_files/nested_iterating.txt b/spec/_files/nested_iterating.txt
new file mode 100644
index 0000000..323fae0
--- /dev/null
+++ b/spec/_files/nested_iterating.txt
@@ -0,0 +1 @@
+foobar
diff --git a/examples/nesting.js b/spec/_files/nesting.js
similarity index 100%
rename from examples/nesting.js
rename to spec/_files/nesting.js
diff --git a/examples/nesting.html b/spec/_files/nesting.mustache
similarity index 100%
rename from examples/nesting.html
rename to spec/_files/nesting.mustache
diff --git a/spec/_files/nesting.txt b/spec/_files/nesting.txt
new file mode 100644
index 0000000..7db34b1
--- /dev/null
+++ b/spec/_files/nesting.txt
@@ -0,0 +1,3 @@
+ 1
+ 2
+ 3
diff --git a/spec/_files/nesting_same_name.js b/spec/_files/nesting_same_name.js
new file mode 100644
index 0000000..a5fb65b
--- /dev/null
+++ b/spec/_files/nesting_same_name.js
@@ -0,0 +1,8 @@
+var nesting_same_name = {
+ items: [
+ {
+ name: 'name',
+ items: [1, 2, 3, 4]
+ }
+ ]
+};
diff --git a/spec/_files/nesting_same_name.mustache b/spec/_files/nesting_same_name.mustache
new file mode 100644
index 0000000..777dbd6
--- /dev/null
+++ b/spec/_files/nesting_same_name.mustache
@@ -0,0 +1 @@
+{{#items}}{{name}}{{#items}}{{.}}{{/items}}{{/items}}
diff --git a/spec/_files/nesting_same_name.txt b/spec/_files/nesting_same_name.txt
new file mode 100644
index 0000000..34fcfd3
--- /dev/null
+++ b/spec/_files/nesting_same_name.txt
@@ -0,0 +1 @@
+name1234
diff --git a/examples/null_string.js b/spec/_files/null_string.js
similarity index 86%
rename from examples/null_string.js
rename to spec/_files/null_string.js
index 93414a1..ab5291f 100644
--- a/examples/null_string.js
+++ b/spec/_files/null_string.js
@@ -3,6 +3,7 @@ var null_string = {
glytch: true,
binary: false,
value: null,
+ undef: undefined,
numeric: function() {
return NaN;
}
diff --git a/examples/null_string.html b/spec/_files/null_string.mustache
similarity index 65%
rename from examples/null_string.html
rename to spec/_files/null_string.mustache
index 12bcb08..a6f3300 100644
--- a/examples/null_string.html
+++ b/spec/_files/null_string.mustache
@@ -2,4 +2,5 @@ Hello {{name}}
glytch {{glytch}}
binary {{binary}}
value {{value}}
-numeric {{numeric}}
\ No newline at end of file
+undef {{undef}}
+numeric {{numeric}}
diff --git a/examples/null_string.txt b/spec/_files/null_string.txt
similarity index 88%
rename from examples/null_string.txt
rename to spec/_files/null_string.txt
index 827569b..bcabe0a 100644
--- a/examples/null_string.txt
+++ b/spec/_files/null_string.txt
@@ -2,4 +2,5 @@ Hello Elise
glytch true
binary false
value
+undef
numeric NaN
diff --git a/spec/_files/partial_array.js b/spec/_files/partial_array.js
new file mode 100644
index 0000000..f16e9eb
--- /dev/null
+++ b/spec/_files/partial_array.js
@@ -0,0 +1,3 @@
+var partial_array = {
+ array: ['1', '2', '3', '4']
+};
\ No newline at end of file
diff --git a/examples/array_partial.html b/spec/_files/partial_array.mustache
similarity index 100%
rename from examples/array_partial.html
rename to spec/_files/partial_array.mustache
diff --git a/examples/array_partial.2.html b/spec/_files/partial_array.partial
similarity index 83%
rename from examples/array_partial.2.html
rename to spec/_files/partial_array.partial
index 235dd5f..0ba652c 100644
--- a/examples/array_partial.2.html
+++ b/spec/_files/partial_array.partial
@@ -1,4 +1,4 @@
Here's a non-sense array of values
{{#array}}
{{.}}
-{{/array}}
\ No newline at end of file
+{{/array}}
diff --git a/examples/array_partial.txt b/spec/_files/partial_array.txt
similarity index 98%
rename from examples/array_partial.txt
rename to spec/_files/partial_array.txt
index fd5f48e..892837c 100644
--- a/examples/array_partial.txt
+++ b/spec/_files/partial_array.txt
@@ -3,4 +3,3 @@ Here's a non-sense array of values
2
3
4
-
diff --git a/examples/array_of_partials_partial.js b/spec/_files/partial_array_of_partials.js
similarity index 61%
rename from examples/array_of_partials_partial.js
rename to spec/_files/partial_array_of_partials.js
index 45611cf..706e7ed 100644
--- a/examples/array_of_partials_partial.js
+++ b/spec/_files/partial_array_of_partials.js
@@ -1,3 +1,3 @@
-var partial_context = {
+var partial_array_of_partials = {
numbers: [{i: '1'}, {i: '2'}, {i: '3'}, {i: '4'}]
};
diff --git a/examples/array_of_partials_implicit_partial.html b/spec/_files/partial_array_of_partials.mustache
similarity index 100%
rename from examples/array_of_partials_implicit_partial.html
rename to spec/_files/partial_array_of_partials.mustache
diff --git a/examples/array_of_partials_partial.2.html b/spec/_files/partial_array_of_partials.partial
similarity index 100%
rename from examples/array_of_partials_partial.2.html
rename to spec/_files/partial_array_of_partials.partial
diff --git a/examples/array_of_partials_implicit_partial.txt b/spec/_files/partial_array_of_partials.txt
similarity index 100%
rename from examples/array_of_partials_implicit_partial.txt
rename to spec/_files/partial_array_of_partials.txt
diff --git a/spec/_files/partial_array_of_partials_implicit.js b/spec/_files/partial_array_of_partials_implicit.js
new file mode 100644
index 0000000..864efa3
--- /dev/null
+++ b/spec/_files/partial_array_of_partials_implicit.js
@@ -0,0 +1,3 @@
+var partial_array_of_partials_implicit = {
+ numbers: ['1', '2', '3', '4']
+};
diff --git a/examples/array_of_partials_partial.html b/spec/_files/partial_array_of_partials_implicit.mustache
similarity index 100%
rename from examples/array_of_partials_partial.html
rename to spec/_files/partial_array_of_partials_implicit.mustache
diff --git a/examples/array_of_partials_implicit_partial.2.html b/spec/_files/partial_array_of_partials_implicit.partial
similarity index 100%
rename from examples/array_of_partials_implicit_partial.2.html
rename to spec/_files/partial_array_of_partials_implicit.partial
diff --git a/examples/array_of_partials_partial.txt b/spec/_files/partial_array_of_partials_implicit.txt
similarity index 100%
rename from examples/array_of_partials_partial.txt
rename to spec/_files/partial_array_of_partials_implicit.txt
diff --git a/spec/_files/partial_empty.js b/spec/_files/partial_empty.js
new file mode 100644
index 0000000..3f36df6
--- /dev/null
+++ b/spec/_files/partial_empty.js
@@ -0,0 +1,3 @@
+var partial_empty = {
+ foo: 1
+};
diff --git a/examples/empty_partial.html b/spec/_files/partial_empty.mustache
similarity index 100%
rename from examples/empty_partial.html
rename to spec/_files/partial_empty.mustache
diff --git a/spec/_files/partial_empty.partial b/spec/_files/partial_empty.partial
new file mode 100644
index 0000000..e69de29
diff --git a/examples/empty_partial.txt b/spec/_files/partial_empty.txt
similarity index 66%
rename from examples/empty_partial.txt
rename to spec/_files/partial_empty.txt
index 90d2b9f..1a67907 100644
--- a/examples/empty_partial.txt
+++ b/spec/_files/partial_empty.txt
@@ -1,2 +1 @@
hey 1
-yo
diff --git a/examples/partial_recursion.js b/spec/_files/partial_recursion.js
similarity index 81%
rename from examples/partial_recursion.js
rename to spec/_files/partial_recursion.js
index ad1f2eb..3094eba 100644
--- a/examples/partial_recursion.js
+++ b/spec/_files/partial_recursion.js
@@ -1,4 +1,4 @@
-var partial_context = {
+var partial_recursion = {
name: '1',
kids: [
{
diff --git a/examples/partial_recursion.html b/spec/_files/partial_recursion.mustache
similarity index 76%
rename from examples/partial_recursion.html
rename to spec/_files/partial_recursion.mustache
index d965a6a..3c3651a 100644
--- a/examples/partial_recursion.html
+++ b/spec/_files/partial_recursion.mustache
@@ -1,4 +1,4 @@
{{name}}
{{#kids}}
{{>partial}}
-{{/kids}}
\ No newline at end of file
+{{/kids}}
diff --git a/examples/partial_recursion.2.html b/spec/_files/partial_recursion.partial
similarity index 72%
rename from examples/partial_recursion.2.html
rename to spec/_files/partial_recursion.partial
index 457d2a0..ba6f8fa 100644
--- a/examples/partial_recursion.2.html
+++ b/spec/_files/partial_recursion.partial
@@ -1,4 +1,4 @@
{{name}}
{{#children}}
{{>partial}}
-{{/children}}
\ No newline at end of file
+{{/children}}
diff --git a/examples/partial_recursion.txt b/spec/_files/partial_recursion.txt
similarity index 100%
rename from examples/partial_recursion.txt
rename to spec/_files/partial_recursion.txt
diff --git a/spec/_files/partial_template.js b/spec/_files/partial_template.js
new file mode 100644
index 0000000..196f1e8
--- /dev/null
+++ b/spec/_files/partial_template.js
@@ -0,0 +1,6 @@
+var partial_template = {
+ title: function() {
+ return "Welcome";
+ },
+ again: "Goodbye"
+};
diff --git a/examples/template_partial.html b/spec/_files/partial_template.mustache
similarity index 59%
rename from examples/template_partial.html
rename to spec/_files/partial_template.mustache
index 6a09372..6a7492e 100644
--- a/examples/template_partial.html
+++ b/spec/_files/partial_template.mustache
@@ -1,2 +1,2 @@
{{title}}
-{{>partial}}
\ No newline at end of file
+{{>partial}}
diff --git a/spec/_files/partial_template.partial b/spec/_files/partial_template.partial
new file mode 100644
index 0000000..a404529
--- /dev/null
+++ b/spec/_files/partial_template.partial
@@ -0,0 +1 @@
+Again, {{again}}!
diff --git a/examples/template_partial.txt b/spec/_files/partial_template.txt
similarity index 100%
rename from examples/template_partial.txt
rename to spec/_files/partial_template.txt
diff --git a/spec/_files/partial_view.js b/spec/_files/partial_view.js
new file mode 100644
index 0000000..4ea8a12
--- /dev/null
+++ b/spec/_files/partial_view.js
@@ -0,0 +1,16 @@
+var partial_view = {
+ greeting: function() {
+ return "Welcome";
+ },
+
+ farewell: function() {
+ return "Fair enough, right?";
+ },
+
+ name: "Chris",
+ value: 10000,
+ taxed_value: function() {
+ return this.value - (this.value * 0.4);
+ },
+ in_ca: true
+};
diff --git a/examples/view_partial.html b/spec/_files/partial_view.mustache
similarity index 61%
rename from examples/view_partial.html
rename to spec/_files/partial_view.mustache
index 8f0c08d..f8f6a5b 100644
--- a/examples/view_partial.html
+++ b/spec/_files/partial_view.mustache
@@ -1,3 +1,3 @@
{{greeting}}
{{>partial}}
-{{farewell}}
\ No newline at end of file
+{{farewell}}
diff --git a/examples/view_partial.2.html b/spec/_files/partial_view.partial
similarity index 100%
rename from examples/view_partial.2.html
rename to spec/_files/partial_view.partial
diff --git a/examples/view_partial.txt b/spec/_files/partial_view.txt
similarity index 99%
rename from examples/view_partial.txt
rename to spec/_files/partial_view.txt
index 160b0b6..c09147c 100644
--- a/examples/view_partial.txt
+++ b/spec/_files/partial_view.txt
@@ -2,5 +2,4 @@
Hello Chris
You have just won $10000!
Well, $6000, after taxes.
-
Fair enough, right?
diff --git a/spec/_files/partial_whitespace.js b/spec/_files/partial_whitespace.js
new file mode 100644
index 0000000..f4b1ee5
--- /dev/null
+++ b/spec/_files/partial_whitespace.js
@@ -0,0 +1,17 @@
+var partial_whitespace = {
+ greeting: function() {
+ return "Welcome";
+ },
+
+ farewell: function() {
+ return "Fair enough, right?";
+ },
+
+ name: "Chris",
+ value: 10000,
+ taxed_value: function() {
+ return this.value - (this.value * 0.4);
+ },
+ in_ca: true
+};
+
diff --git a/examples/whitespace_partial.html b/spec/_files/partial_whitespace.mustache
similarity index 63%
rename from examples/whitespace_partial.html
rename to spec/_files/partial_whitespace.mustache
index ce43cb3..48bd1ff 100644
--- a/examples/whitespace_partial.html
+++ b/spec/_files/partial_whitespace.mustache
@@ -1,3 +1,3 @@
{{ greeting }}
{{> partial }}
-{{ farewell }}
\ No newline at end of file
+{{ farewell }}
diff --git a/examples/whitespace_partial.2.html b/spec/_files/partial_whitespace.partial
similarity index 100%
rename from examples/whitespace_partial.2.html
rename to spec/_files/partial_whitespace.partial
diff --git a/examples/whitespace_partial.txt b/spec/_files/partial_whitespace.txt
similarity index 99%
rename from examples/whitespace_partial.txt
rename to spec/_files/partial_whitespace.txt
index 160b0b6..c09147c 100644
--- a/examples/whitespace_partial.txt
+++ b/spec/_files/partial_whitespace.txt
@@ -2,5 +2,4 @@
Hello Chris
You have just won $10000!
Well, $6000, after taxes.
-
Fair enough, right?
diff --git a/examples/recursion_with_same_names.js b/spec/_files/recursion_with_same_names.js
similarity index 100%
rename from examples/recursion_with_same_names.js
rename to spec/_files/recursion_with_same_names.js
diff --git a/examples/recursion_with_same_names.html b/spec/_files/recursion_with_same_names.mustache
similarity index 100%
rename from examples/recursion_with_same_names.html
rename to spec/_files/recursion_with_same_names.mustache
diff --git a/examples/recursion_with_same_names.txt b/spec/_files/recursion_with_same_names.txt
similarity index 96%
rename from examples/recursion_with_same_names.txt
rename to spec/_files/recursion_with_same_names.txt
index c23bb65..cb15d75 100644
--- a/examples/recursion_with_same_names.txt
+++ b/spec/_files/recursion_with_same_names.txt
@@ -1,5 +1,6 @@
name
desc
+
t1
0
t2
diff --git a/examples/reuse_of_enumerables.js b/spec/_files/reuse_of_enumerables.js
similarity index 70%
rename from examples/reuse_of_enumerables.js
rename to spec/_files/reuse_of_enumerables.js
index 543e121..a3168d3 100644
--- a/examples/reuse_of_enumerables.js
+++ b/spec/_files/reuse_of_enumerables.js
@@ -1,6 +1,6 @@
var reuse_of_enumerables = {
terms: [
{name: 't1', index: 0},
- {name: 't2', index: 1},
+ {name: 't2', index: 1}
]
};
\ No newline at end of file
diff --git a/examples/reuse_of_enumerables.html b/spec/_files/reuse_of_enumerables.mustache
similarity index 100%
rename from examples/reuse_of_enumerables.html
rename to spec/_files/reuse_of_enumerables.mustache
diff --git a/examples/reuse_of_enumerables.txt b/spec/_files/reuse_of_enumerables.txt
similarity index 100%
rename from examples/reuse_of_enumerables.txt
rename to spec/_files/reuse_of_enumerables.txt
diff --git a/examples/section_as_context.js b/spec/_files/section_as_context.js
similarity index 100%
rename from examples/section_as_context.js
rename to spec/_files/section_as_context.js
diff --git a/examples/section_as_context.html b/spec/_files/section_as_context.mustache
similarity index 100%
rename from examples/section_as_context.html
rename to spec/_files/section_as_context.mustache
diff --git a/examples/section_as_context.txt b/spec/_files/section_as_context.txt
similarity index 54%
rename from examples/section_as_context.txt
rename to spec/_files/section_as_context.txt
index 53ee336..d834e80 100644
--- a/examples/section_as_context.txt
+++ b/spec/_files/section_as_context.txt
@@ -1,6 +1,6 @@
this is an object
one of its attributes is a list
- - listitem1
- - listitem2
-
+ listitem1
+ listitem2
+
diff --git a/examples/simple.js b/spec/_files/simple.js
similarity index 100%
rename from examples/simple.js
rename to spec/_files/simple.js
diff --git a/examples/simple.html b/spec/_files/simple.mustache
similarity index 89%
rename from examples/simple.html
rename to spec/_files/simple.mustache
index 03df206..2fea632 100644
--- a/examples/simple.html
+++ b/spec/_files/simple.mustache
@@ -2,4 +2,4 @@ Hello {{name}}
You have just won ${{value}}!
{{#in_ca}}
Well, ${{ taxed_value }}, after taxes.
-{{/in_ca}}
\ No newline at end of file
+{{/in_ca}}
diff --git a/examples/simple.txt b/spec/_files/simple.txt
similarity index 100%
rename from examples/simple.txt
rename to spec/_files/simple.txt
diff --git a/spec/_files/string_as_context.js b/spec/_files/string_as_context.js
new file mode 100644
index 0000000..784368b
--- /dev/null
+++ b/spec/_files/string_as_context.js
@@ -0,0 +1,4 @@
+var string_as_context = {
+ a_string: 'aa',
+ a_list: ['a','b','c']
+};
diff --git a/spec/_files/string_as_context.mustache b/spec/_files/string_as_context.mustache
new file mode 100644
index 0000000..c6aa11a
--- /dev/null
+++ b/spec/_files/string_as_context.mustache
@@ -0,0 +1,5 @@
+
+{{#a_list}}
+ - {{.}}
+{{/a_list}}
+
\ No newline at end of file
diff --git a/spec/_files/string_as_context.txt b/spec/_files/string_as_context.txt
new file mode 100644
index 0000000..35e6306
--- /dev/null
+++ b/spec/_files/string_as_context.txt
@@ -0,0 +1,5 @@
+
\ No newline at end of file
diff --git a/examples/two_in_a_row.js b/spec/_files/two_in_a_row.js
similarity index 100%
rename from examples/two_in_a_row.js
rename to spec/_files/two_in_a_row.js
diff --git a/spec/_files/two_in_a_row.mustache b/spec/_files/two_in_a_row.mustache
new file mode 100644
index 0000000..b23f29e
--- /dev/null
+++ b/spec/_files/two_in_a_row.mustache
@@ -0,0 +1 @@
+{{greeting}}, {{name}}!
diff --git a/examples/two_in_a_row.txt b/spec/_files/two_in_a_row.txt
similarity index 100%
rename from examples/two_in_a_row.txt
rename to spec/_files/two_in_a_row.txt
diff --git a/examples/two_sections.js b/spec/_files/two_sections.js
similarity index 100%
rename from examples/two_sections.js
rename to spec/_files/two_sections.js
diff --git a/examples/two_sections.html b/spec/_files/two_sections.mustache
similarity index 100%
rename from examples/two_sections.html
rename to spec/_files/two_sections.mustache
diff --git a/spec/_files/two_sections.txt b/spec/_files/two_sections.txt
new file mode 100644
index 0000000..e69de29
diff --git a/examples/unescaped.js b/spec/_files/unescaped.js
similarity index 100%
rename from examples/unescaped.js
rename to spec/_files/unescaped.js
diff --git a/spec/_files/unescaped.mustache b/spec/_files/unescaped.mustache
new file mode 100644
index 0000000..6b07d7b
--- /dev/null
+++ b/spec/_files/unescaped.mustache
@@ -0,0 +1 @@
+{{{title}}}
diff --git a/examples/unescaped.txt b/spec/_files/unescaped.txt
similarity index 100%
rename from examples/unescaped.txt
rename to spec/_files/unescaped.txt
diff --git a/spec/_files/whitespace.js b/spec/_files/whitespace.js
new file mode 100644
index 0000000..97f53c1
--- /dev/null
+++ b/spec/_files/whitespace.js
@@ -0,0 +1,4 @@
+var whitespace = {
+ tag1: "Hello",
+ tag2: "World"
+};
diff --git a/spec/_files/whitespace.mustache b/spec/_files/whitespace.mustache
new file mode 100644
index 0000000..aa76e08
--- /dev/null
+++ b/spec/_files/whitespace.mustache
@@ -0,0 +1,4 @@
+{{tag1}}
+
+
+{{tag2}}.
diff --git a/spec/_files/whitespace.txt b/spec/_files/whitespace.txt
new file mode 100644
index 0000000..851fa74
--- /dev/null
+++ b/spec/_files/whitespace.txt
@@ -0,0 +1,4 @@
+Hello
+
+
+World.
diff --git a/spec/mustache_spec.rb b/spec/mustache_spec.rb
new file mode 100644
index 0000000..3c27d80
--- /dev/null
+++ b/spec/mustache_spec.rb
@@ -0,0 +1,218 @@
+require 'rubygems'
+require 'json'
+
+ROOT = File.expand_path('../..', __FILE__)
+SPEC = File.join(ROOT, 'spec')
+FILES = File.join(SPEC, '_files')
+
+MUSTACHE = File.read(File.join(ROOT, "mustache.js"))
+
+TESTS = Dir.glob(File.join(FILES, '*.js')).map do |name|
+ File.basename name, '.js'
+end
+
+NODE_PATH = `which node`.strip
+JS_PATH = `which js`.strip
+JSC_PATH = "/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc"
+RHINO_JAR = "org.mozilla.javascript.tools.shell.Main"
+
+def load_test(name)
+ template = File.read(File.join(FILES, "#{name}.mustache"))
+ view = File.read(File.join(FILES, "#{name}.js"))
+ partial_file = File.join(FILES, "#{name}.partial")
+ partial = if File.exist?(partial_file)
+ File.read(partial_file)
+ end
+ expect = File.read(File.join(FILES, "#{name}.txt"))
+
+ [template, view, partial, expect]
+end
+
+def run_js(runner, js)
+ cmd = case runner
+ when :spidermonkey
+ JS_PATH
+ when :jsc
+ JSC_PATH
+ when :rhino
+ "java #{RHINO_JAR}"
+ when :v8
+ NODE_PATH
+ end
+
+ runner_file = "runner.js"
+ File.open(runner_file, 'w') {|file| file.write(js) }
+ `#{cmd} #{runner_file}`
+ensure
+ FileUtils.rm_r(runner_file)
+end
+
+$engines_run = 0
+
+describe "mustache" do
+ shared_examples_for "mustache rendering" do
+ before(:all) do
+ $engines_run += 1
+ end
+
+ it "should return the same result when invoked multiple times" do
+ js = <<-JS
+ #{@boilerplate}
+ Mustache.render("x")
+ print(Mustache.render("x"));
+ JS
+
+ run_js(@runner, js).should == "x\n"
+ end
+
+ it "should clear the context after each run" do
+ js = <<-JS
+ #{@boilerplate}
+ Mustache.render("{{#list}}{{x}}{{/list}}", {list: [{x: 1}]})
+ try {
+ print(Mustache.render("{{#list}}{{x}}{{/list}}", {list: [{}]}));
+ } catch(e) {
+ print('ERROR: ' + e.message);
+ }
+ JS
+
+ run_js(@runner, js).should == "\n"
+ end
+
+ TESTS.each do |test|
+ describe test do
+ it "should render the correct output" do
+ template, view, partial, expect = load_test(test)
+
+ js = <<-JS
+ try {
+ #{@boilerplate}
+ var template = #{template.to_json};
+ #{view}
+ var partials = {partial: #{partial ? partial.to_json : '""'}};
+ print(Mustache.render(template, #{test}, partials));
+ } catch(e) {
+ print('ERROR: ' + e.message);
+ }
+ JS
+
+ run_js(@runner, js).chomp.should == expect
+ end
+
+ # it "should send the correct output" do
+ # template, view, partial, expect = load_test(test)
+ #
+ # js = <<-JS
+ # try {
+ # #{@boilerplate}
+ # var template = #{template.to_json};
+ # #{view}
+ # var partials = {
+ # "partial": #{(partial || '').to_json}
+ # };
+ # var buffer = [];
+ # var send = function (chunk) {
+ # buffer.push(chunk);
+ # };
+ # Mustache.render(template, #{test}, partials, send);
+ # print(buffer.join(""));
+ # } catch(e) {
+ # print('ERROR: ' + e.message);
+ # }
+ # JS
+ #
+ # run_js(@runner, js).chomp.should == expect
+ # end
+ end
+ end
+ end
+
+ context "running in V8 (Chrome, node)" do
+ if File.exist?(NODE_PATH)
+ before(:all) do
+ $stdout.write "Testing in V8 "
+ @runner = :v8
+ @boilerplate = MUSTACHE.dup
+ @boilerplate << <<-JS
+ var print = console.log;
+ JS
+ end
+
+ after(:all) do
+ puts " Done!"
+ end
+
+ it_should_behave_like "mustache rendering"
+ else
+ puts "Skipping tests in V8 (node not found)"
+ end
+ end
+
+ context "running in SpiderMonkey (Mozilla, Firefox)" do
+ if File.exist?(JS_PATH)
+ before(:all) do
+ $stdout.write "Testing in SpiderMonkey "
+ @runner = :spidermonkey
+ @boilerplate = MUSTACHE.dup
+ end
+
+ after(:all) do
+ puts " Done!"
+ end
+
+ it_should_behave_like "mustache rendering"
+ else
+ puts "Skipping tests in SpiderMonkey (js not found)"
+ end
+ end
+
+ context "running in JavaScriptCore (WebKit, Safari)" do
+ if File.exist?(JSC_PATH)
+ before(:all) do
+ $stdout.write "Testing in JavaScriptCore "
+ @runner = :jsc
+ @boilerplate = MUSTACHE.dup
+ end
+
+ after(:all) do
+ puts " Done!"
+ end
+
+ it_should_behave_like "mustache rendering"
+ else
+ puts "Skipping tests in JavaScriptCore (jsc not found)"
+ end
+ end
+
+ context "running in Rhino (Mozilla, Java)" do
+ if `java #{RHINO_JAR} 'foo' 2>&1` !~ /ClassNotFoundException/
+ before(:all) do
+ $stdout.write "Testing in Rhino "
+ @runner = :rhino
+ @boilerplate = MUSTACHE.dup
+ end
+
+ after(:all) do
+ puts " Done!"
+ end
+
+ it_should_behave_like "mustache rendering"
+ else
+ puts "Skipping tests in Rhino (JAR #{RHINO_JAR} was not found)"
+ end
+ end
+
+ context "suite" do
+ before(:each) do
+ $stdout.write "Verifying that we ran the tests in at least one engine ... "
+ end
+
+ after(:each) do
+ puts @exception.nil? ? "OK" : "ERROR"
+ end
+
+ it "should have run at least one time" do
+ $engines_run.should > 0
+ end
+ end
+end
diff --git a/test/mustache_spec.rb b/test/mustache_spec.rb
deleted file mode 100644
index 5ce8bea..0000000
--- a/test/mustache_spec.rb
+++ /dev/null
@@ -1,271 +0,0 @@
-require 'rubygems'
-require 'json'
-
-__DIR__ = File.dirname(__FILE__)
-
-testnames = Dir.glob(__DIR__ + '/../examples/*.js').map do |name|
- File.basename name, '.js'
-end
-
-non_partials = testnames.select{|t| not t.include? "partial"}
-partials = testnames.select{|t| t.include? "partial"}
-
-def load_test(dir, name, partial=false)
- view = File.read(dir + "/../examples/#{name}.js")
- template = File.read(dir + "/../examples/#{name}.html").to_json
- expect = File.read(dir + "/../examples/#{name}.txt")
- if not partial
- [view, template, expect]
- else
- partial = File.read(dir + "/../examples/#{name}.2.html").to_json
- [view, template, partial, expect]
- end
-end
-
-JS_PATH = `which js`.strip()
-JSC_PATH = "/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc"
-RHINO_JAR = "org.mozilla.javascript.tools.shell.Main"
-
-engines_run = 0
-
-describe "mustache" do
-
- shared_examples_for "Mustache rendering" do
-
- before(:all) do
- engines_run += 1
- mustache = File.read(__DIR__ + "/../mustache.js")
- stubbed_gettext = <<-JS
- // Stubbed gettext translation method for {{_i}}{{/i}} tags in Mustache.
- function _(params) {
- if (typeof params === "string") {
- return params
- }
-
- return params.text;
- }
- JS
-
- @boilerplate = <<-JS
- #{mustache}
- #{stubbed_gettext}
- JS
- end
-
- it "should return the same when invoked multiple times" do
- js = <<-JS
- #{@boilerplate}
- Mustache.to_html("x")
- print(Mustache.to_html("x"));
- JS
- run_js(@run_js, js).should == "x\n"
-
- end
-
- it "should clear the context after each run" do
- js = <<-JS
- #{@boilerplate}
- Mustache.to_html("{{#list}}{{x}}{{/list}}", {list: [{x: 1}]})
- try {
- print(Mustache.to_html("{{#list}}{{x}}{{/list}}", {list: [{}]}));
- } catch(e) {
- print('ERROR: ' + e.message);
- }
- JS
- run_js(@run_js, js).should == "\n"
- end
-
- non_partials.each do |testname|
- describe testname do
- it "should generate the correct html" do
-
- view, template, expect = load_test(__DIR__, testname)
-
- runner = <<-JS
- try {
- #{@boilerplate}
- #{view}
- var template = #{template};
- var result = Mustache.to_html(template, #{testname});
- print(result);
- } catch(e) {
- print('ERROR: ' + e.message);
- }
- JS
-
- run_js(@run_js, runner).should == expect
- end
- it "should sendFun the correct html" do
-
- view, template, expect = load_test(__DIR__, testname)
-
- runner = <<-JS
- try {
- #{@boilerplate}
- #{view}
- var chunks = [];
- var sendFun = function(chunk) {
- if (chunk != "") {
- chunks.push(chunk);
- }
- }
- var template = #{template};
- Mustache.to_html(template, #{testname}, null, sendFun);
- print(chunks.join("\\n"));
- } catch(e) {
- print('ERROR: ' + e.message);
- }
- JS
-
- run_js(@run_js, runner).strip.should == expect.strip
- end
- end
- end
-
- partials.each do |testname|
- describe testname do
- it "should generate the correct html" do
-
- view, template, partial, expect =
- load_test(__DIR__, testname, true)
-
- runner = <<-JS
- try {
- #{@boilerplate}
- #{view}
- var template = #{template};
- var partials = {"partial": #{partial}};
- var result = Mustache.to_html(template, partial_context, partials);
- print(result);
- } catch(e) {
- print('ERROR: ' + e.message);
- }
- JS
-
- run_js(@run_js, runner).should == expect
- end
- it "should sendFun the correct html" do
-
- view, template, partial, expect =
- load_test(__DIR__, testname, true)
-
- runner = <<-JS
- try {
- #{@boilerplate}
- #{view};
- var template = #{template};
- var partials = {"partial": #{partial}};
- var chunks = [];
- var sendFun = function(chunk) {
- if (chunk != "") {
- chunks.push(chunk);
- }
- }
- Mustache.to_html(template, partial_context, partials, sendFun);
- print(chunks.join("\\n"));
- } catch(e) {
- print('ERROR: ' + e.message);
- }
- JS
-
- run_js(@run_js, runner).strip.should == expect.strip
- end
- end
- end
- end
-
- context "running in SpiderMonkey (Mozilla, Firefox)" do
- p JS_PATH
- if !JS_PATH.empty?
- before(:each) do
- @run_js = :run_js_js
- end
-
- before(:all) do
- puts "\nTesting mustache.js in SpiderMonkey:\n"
- end
-
- after(:all) do
- puts "\nDone\n"
- end
-
- it_should_behave_like "Mustache rendering"
- else
- puts "\nSkipping tests in SpiderMonkey (js not found)\n"
- end
- end
-
- context "running in JavaScriptCore (WebKit, Safari)" do
- if File.exists?(JSC_PATH)
- before(:each) do
- @run_js = :run_js_jsc
- end
-
- before(:all) do
- puts "\nTesting mustache.js in JavaScriptCore:\n"
- end
-
- after(:all) do
- puts "\nDone\n"
- end
-
- it_should_behave_like "Mustache rendering"
- else
- puts "\nSkipping tests in JavaScriptCore (jsc not found)\n"
- end
- end
-
- context "running in Rhino (Mozilla)" do
- if !`java #{RHINO_JAR} 'foo' 2>&1`.match(/ClassNotFoundException/)
- before(:each) do
- @run_js = :run_js_rhino
- end
-
- before(:all) do
- puts "\nTesting mustache.js in Rhino:\n"
- end
-
- after(:all) do
- puts "\nDone\n"
- end
-
- it_should_behave_like "Mustache rendering"
- else
- puts "\nSkipping tests in Rhino (JAR #{RHINO_JAR} was not found)\n"
- end
- end
-
- context "suite" do
- before(:all) do
- puts "\nVerifying that we ran at the tests in at least one engine\n"
- end
-
- after(:all) do
- puts "\nDone\n"
- end
-
- it "should have run at least one time" do
- engines_run.should > 0
- end
- end
-
- def run_js(runner, js)
- send(runner, js)
- end
-
- def run_js_js(js)
- File.open("runner.js", 'w') {|f| f << js}
- `#{JS_PATH} runner.js`
- end
-
- def run_js_jsc(js)
- File.open("runner.js", 'w') {|f| f << js}
- `#{JSC_PATH} runner.js`
- end
-
- def run_js_rhino(js)
- File.open("runner.js", 'w') {|f| f << js}
- `java #{RHINO_JAR} runner.js`
- end
-end
-
diff --git a/wrappers/dojo/mustache.js.post b/wrappers/dojo/mustache.js.post
new file mode 100644
index 0000000..eeeb4b7
--- /dev/null
+++ b/wrappers/dojo/mustache.js.post
@@ -0,0 +1,4 @@
+
+ dojox.mustache = dojo.hitch(Mustache, "render");
+
+})();
\ No newline at end of file
diff --git a/mustache-dojo/mustache.js.tpl.pre b/wrappers/dojo/mustache.js.pre
similarity index 100%
rename from mustache-dojo/mustache.js.tpl.pre
rename to wrappers/dojo/mustache.js.pre
diff --git a/wrappers/jquery/mustache.js.post b/wrappers/jquery/mustache.js.post
new file mode 100644
index 0000000..d27d730
--- /dev/null
+++ b/wrappers/jquery/mustache.js.post
@@ -0,0 +1,14 @@
+
+ $.mustache = function (template, view, partials) {
+ return Mustache.render(template, view, partials);
+ };
+
+ $.fn.mustache = function (view, partials) {
+ return $(this).map(function (i, elm) {
+ var template = $(elm).html().trim();
+ var output = $.mustache(template, view, partials);
+ return $(output).get();
+ });
+ };
+
+})(jQuery);
diff --git a/mustache-jquery/jquery.mustache.js.tpl.pre b/wrappers/jquery/mustache.js.pre
similarity index 100%
rename from mustache-jquery/jquery.mustache.js.tpl.pre
rename to wrappers/jquery/mustache.js.pre
diff --git a/wrappers/mootools/mustache.js.post b/wrappers/mootools/mustache.js.post
new file mode 100644
index 0000000..aa9b8fa
--- /dev/null
+++ b/wrappers/mootools/mustache.js.post
@@ -0,0 +1,5 @@
+
+ Object.implement('mustache', function(view, partials){
+ return Mustache.render(view, this, partials);
+ });
+})();
diff --git a/wrappers/mootools/mustache.js.pre b/wrappers/mootools/mustache.js.pre
new file mode 100644
index 0000000..9839f99
--- /dev/null
+++ b/wrappers/mootools/mustache.js.pre
@@ -0,0 +1,2 @@
+(function(){
+
diff --git a/mustache-qooxdoo/qooxdoo.mustache.js.tpl.post b/wrappers/qooxdoo/mustache.js.post
similarity index 74%
rename from mustache-qooxdoo/qooxdoo.mustache.js.tpl.post
rename to wrappers/qooxdoo/mustache.js.post
index e66f9e6..bd4b406 100644
--- a/mustache-qooxdoo/qooxdoo.mustache.js.tpl.post
+++ b/wrappers/qooxdoo/mustache.js.post
@@ -4,6 +4,6 @@
// EXPOSE qooxdoo variant
qx.bom.Template.version = Mustache.version;
-qx.bom.Template.toHtml = Mustache.to_html;
+qx.bom.Template.toHtml = Mustache.render;
})();
diff --git a/mustache-qooxdoo/qooxdoo.mustache.js.tpl.pre b/wrappers/qooxdoo/mustache.js.pre
similarity index 99%
rename from mustache-qooxdoo/qooxdoo.mustache.js.tpl.pre
rename to wrappers/qooxdoo/mustache.js.pre
index bc3ea7f..f1fce6d 100644
--- a/mustache-qooxdoo/qooxdoo.mustache.js.tpl.pre
+++ b/wrappers/qooxdoo/mustache.js.pre
@@ -19,7 +19,7 @@
This class contains code based on the following work:
- * Mustache.js version 0.4.0-dev
+ * Mustache.js version 0.5.0-dev
Code:
https://github.com/janl/mustache.js
diff --git a/mustache-requirejs/requirejs.mustache.js.tpl.post b/wrappers/requirejs/mustache.js.post
similarity index 100%
rename from mustache-requirejs/requirejs.mustache.js.tpl.post
rename to wrappers/requirejs/mustache.js.post
diff --git a/mustache-requirejs/requirejs.mustache.js.tpl.pre b/wrappers/requirejs/mustache.js.pre
similarity index 100%
rename from mustache-requirejs/requirejs.mustache.js.tpl.pre
rename to wrappers/requirejs/mustache.js.pre
diff --git a/wrappers/yui3/mustache.js.post b/wrappers/yui3/mustache.js.post
new file mode 100644
index 0000000..3decbf8
--- /dev/null
+++ b/wrappers/yui3/mustache.js.post
@@ -0,0 +1,4 @@
+
+ Y.mustache = Mustache.render;
+
+}, "0");
diff --git a/mustache-yui3/mustache.js.tpl.pre b/wrappers/yui3/mustache.js.pre
similarity index 100%
rename from mustache-yui3/mustache.js.tpl.pre
rename to wrappers/yui3/mustache.js.pre