Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b29eb6f
commit 9569ea3
Showing
57 changed files
with
1,352 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
Copyright (c) 2010 Ray Christopher Morgan | ||
|
||
Permission is hereby granted, free of charge, to any person | ||
obtaining a copy of this software and associated documentation | ||
files (the "Software"), to deal in the Software without | ||
restriction, including without limitation the rights to use, | ||
copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the | ||
Software is furnished to do so, subject to the following | ||
conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | ||
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | ||
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||
OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
Mu - Mustache template compiler for Node.js | ||
=========================================== | ||
|
||
Mustache is a simply awesome template language inspired by | ||
[ctemplate](http://code.google.com/p/google-ctemplate/) and | ||
[et](http://www.ivan.fomichev.name/2008/05/erlang-template-engine-prototype.html). | ||
|
||
Mu is a Mustache based template engine for Node.js. Mu compiles mustache | ||
templates into an extremely fast executable function. | ||
|
||
|
||
What makes Mu cool? | ||
------------------- | ||
|
||
* It is very fast | ||
* Supports async parsing/compiling | ||
* Rendering is streamed | ||
|
||
|
||
Benchmarks | ||
---------- | ||
|
||
Rendering examples/complex.html.mu 1 million times yields the following results: | ||
|
||
Ruby Mustache - 112 secs (benchmarks/rb/complex_view.rb) | ||
Mu - 40 secs (benchmarks/million_complex.js) | ||
|
||
Tested on a 2.4 GHz Intel Core 2 Duo MacBook Pro | ||
|
||
Mu sync rendering was benchmarking in at 24 secs, but I felt it was much more | ||
important to stream the rendering. Streaming adds a pretty set overhead of | ||
about 14µs (microseconds) to each template render to setup the event emitter. | ||
It also adds a variable extra amount of time due to the additional function calls. | ||
The million_complex.js caused 2µs per render addition. | ||
|
||
|
||
Usage (from demo.js) | ||
-------------------- | ||
|
||
var sys = require('sys'); | ||
var Mu = require('./lib/mu'); | ||
|
||
Mu.templateRoot = './examples'; | ||
|
||
var ctx = { | ||
name: "Chris", | ||
value: 10000, | ||
taxed_value: function() { | ||
return this.value - (this.value * 0.4); | ||
}, | ||
in_ca: true | ||
}; | ||
|
||
Mu.render('simple.html', ctx, {}, function (err, output) { | ||
if (err) { | ||
throw err; | ||
} | ||
var buffer = ''; | ||
|
||
output.addListener('data', function (c) {buffer += c; }) | ||
.addListener('end', function () { sys.puts(buffer); }); | ||
}); | ||
|
||
|
||
Which yields: | ||
|
||
Hello Chris | ||
You have just won $10000! | ||
Well, $6000, after taxes. | ||
|
||
Using Mu.compileText | ||
-------------------- | ||
|
||
var sys = require('sys'); | ||
var Mu = require('./lib/mu'); | ||
|
||
var tmpl = "Hello {{> part}}. Your name is: {{name}}!"; | ||
var partials = {part: "World"}; | ||
var compiled = Mu.compileText(tmpl, partials); | ||
|
||
compiled({name: "Chris"}) | ||
.addListener('data', function (c) { sys.puts(c) }); | ||
|
||
|
||
Mustache Documentation | ||
---------------------- | ||
|
||
See **Tag Types** section at | ||
[http://github.com/defunkt/mustache/](http://github.com/defunkt/mustache/) | ||
for more information on supported tags. | ||
|
||
Todo | ||
---- | ||
|
||
* Better parse time errors. Currently they are decent when partials are not involved | ||
but break down once partials are involved. | ||
* Implement some compile time optimizations. The big one is predetermining when a | ||
enumerable actually needs to inherit the full context. Cutting this out can be huge. | ||
* Cleanup the Preprocessor methods. They are a bit unwieldy. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
var sys = require('sys') | ||
fs = require('fs'), | ||
Path = require('path'); | ||
var Mu = require('../lib/mu'); | ||
|
||
Mu.templateRoot = ''; | ||
|
||
var name = Path.join(Path.dirname(__filename), "../examples/complex"); | ||
var js = fs.readFileSync(name + '.js'); | ||
|
||
js = eval('(' + js + ')'); | ||
|
||
sys.puts(name + '.html'); | ||
|
||
Mu.compile(name + '.html', function (err, compiled) { | ||
if (err) { | ||
throw err; | ||
} | ||
|
||
var buffer = ''; | ||
compiled(js).addListener('data', function (c) { buffer += c; }) | ||
.addListener('end', function () { sys.puts(buffer); }); | ||
|
||
var i = 0; | ||
var d = new Date(); | ||
|
||
(function go() { | ||
if (i++ < 1000000) { | ||
compiled(js).addListener('end', function () { go(); }); | ||
} | ||
})(); | ||
|
||
process.addListener('exit', function () { | ||
sys.error("Time taken: " + ((new Date() - d) / 1000) + "secs"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<h1>{{header}}</h1> | ||
{{#list}} | ||
<ul> | ||
{{#item}} | ||
{{#current}} | ||
<li><strong>{{name}}</strong></li> | ||
{{/current}} | ||
{{#link}} | ||
<li><a href="{{url}}">{{name}}</a></li> | ||
{{/link}} | ||
{{/item}} | ||
</ul> | ||
{{/list}} | ||
{{#empty}} | ||
<p>The list is empty.</p> | ||
{{/empty}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
$LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib' | ||
require 'rubygems' | ||
require 'mustache' | ||
|
||
class ComplexView < Mustache | ||
self.path = File.dirname(__FILE__) | ||
|
||
def header | ||
"Colors" | ||
end | ||
|
||
def item | ||
items = [] | ||
items << { :name => 'red', :current => true, :url => '#Red' } | ||
items << { :name => 'green', :current => false, :url => '#Green' } | ||
items << { :name => 'blue', :current => false, :url => '#Blue' } | ||
items | ||
end | ||
|
||
def link | ||
not self[:current] | ||
end | ||
|
||
def list | ||
not item.empty? | ||
end | ||
|
||
def empty | ||
item.empty? | ||
end | ||
end | ||
|
||
if $0 == __FILE__ | ||
puts ComplexView.to_html | ||
|
||
d = Time.now | ||
1000000.times { | ||
ComplexView.to_html | ||
} | ||
diff = (Time.now - d) | ||
puts "Time taken: " + diff.to_s | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
var sys = require('sys'); | ||
var Mu = require('./lib/mu'); | ||
|
||
Mu.templateRoot = './examples'; | ||
|
||
var ctx = { | ||
name: "Chris", | ||
value: 10000, | ||
taxed_value: function() { | ||
return this.value - (this.value * 0.4); | ||
}, | ||
in_ca: true | ||
}; | ||
|
||
sys.puts("Outputting examples/simple.html.mu with a chunkSize of 10\n"); | ||
|
||
Mu.render('simple.html', ctx, {chunkSize: 10}, function (err, output) { | ||
if (err) { | ||
throw err; | ||
} | ||
|
||
var buffer = ''; | ||
|
||
output | ||
.addListener('data', function (c) { | ||
sys.print(c); // output chunk | ||
output.pause(); // pause for demo | ||
setTimeout(function () { // wait 500ms and resume for demo | ||
output.resume(); | ||
}, 500); | ||
}) | ||
.addListener('end', function () { sys.puts("\n\nDONE"); }); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<h1>{{title}}{{! just something interesting... or not... }}</h1> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
title: function() { | ||
return "A Comedy of Errors"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<h1>A Comedy of Errors</h1> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<h1>{{header}}</h1> | ||
{{#list}} | ||
<ul> | ||
{{#item}} | ||
{{#current}} | ||
<li><strong>{{name}}</strong></li> | ||
{{/current}} | ||
{{#link}} | ||
<li><a href="{{url}}">{{name}}</a></li> | ||
{{/link}} | ||
{{/item}} | ||
</ul> | ||
{{/list}} | ||
{{#empty}} | ||
<p>The list is empty.</p> | ||
{{/empty}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
header: function() { | ||
return "Colors"; | ||
}, | ||
item: [ | ||
{name: "red", current: true, url: "#Red"}, | ||
{name: "green", current: false, url: "#Green"}, | ||
{name: "blue", current: false, url: "#Blue"} | ||
], | ||
link: function() { | ||
return this["current"] !== true; | ||
}, | ||
list: function() { | ||
return this.item.length !== 0; | ||
}, | ||
empty: function() { | ||
return this.item.length === 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<h1>Colors</h1> | ||
<ul> | ||
<li><strong>red</strong></li> | ||
<li><a href="#Green">green</a></li> | ||
<li><a href="#Blue">blue</a></li> | ||
</ul> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<h1>First: {{title}}</h1> | ||
{{>partial.html}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
title: "Welcome" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<h1>First: Welcome</h1> | ||
<h1>Welcome</h1> | ||
<p class="again">Again, Welcome!</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{{=<% %>=}}* <% first %> | ||
* <% second %> | ||
<%=| |=%> | ||
* | third | | ||
|={{ }}=| | ||
* {{ fourth }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
first: "It worked the first time.", | ||
second: "And it worked the second time.", | ||
third: "Then, surprisingly, it worked the third time.", | ||
fourth: "Fourth time also fine!." | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
* It worked the first time. | ||
* And it worked the second time. | ||
|
||
* Then, surprisingly, it worked the third time. | ||
|
||
* Fourth time also fine!. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{{foo}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{bar: 2} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<h1>{{title}}</h1> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
title: function() { | ||
return "Bear > Shark"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<h1>Bear > Shark</h1> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{{#person}} | ||
Name: {{name}} | ||
{{/person}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
person: { | ||
name: "Chris" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
Name: Chris |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<p class="again">Again, {{title}}!</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{{#admin}} | ||
Admin. | ||
{{/admin}} | ||
{{^admin}} | ||
Not Admin. | ||
{{/admin}} |
Oops, something went wrong.