Skip to content

Commit

Permalink
Initial commit - version 0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
RayMorgan committed Jan 26, 2010
0 parents commit 033e607
Show file tree
Hide file tree
Showing 52 changed files with 1,075 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
60 changes: 60 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
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.


Benchmarks
----------

Rendering examples/complex.html.mu 1 million times yields the following results:

Ruby Mustache - 112 secs (benchmarks/rb/complex_view.rb)
Mu - 24 secs (benchmarks/million_complex.js)

Tested on a 2.4 GHz Intel Core 2 Duo MacBook Pro


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)
.addCallback(function (output) {
sys.puts(output);
})
.addErrback(function (e) {
sys.puts(e.stack);
});

Which yields:

Hello Chris
You have just won $10000!
Well, $6000, after taxes.


Mustache Documentation
----------------------

See **Tag Types** section at
[http://github.com/defunkt/mustache/](http://github.com/defunkt/mustache/)
for more information on supported tags.
23 changes: 23 additions & 0 deletions benchmarks/million_complex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
var sys = require('sys')
posix = require('posix'),
Path = require('path');
var Mu = require('../lib/mu');

Mu.templateRoot = '';

var name = Path.join(Path.dirname(__filename), "../examples/complex");
var js = posix.cat(name + '.js').wait();

js = eval('(' + js + ')');

sys.puts(name + '.html')

var compiled = Mu.compile(name + '.html').wait();

sys.puts(compiled(js));

var d = new Date();
for (var i = 0; i < 1000000; i++) {
compiled(js);
}
sys.puts("Time taken: " + ((new Date() - d) / 1000) + "secs");
16 changes: 16 additions & 0 deletions benchmarks/rb/complex_view.mustache
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}}
42 changes: 42 additions & 0 deletions benchmarks/rb/complex_view.rb
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
21 changes: 21 additions & 0 deletions demo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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)
.addCallback(function (output) {
sys.puts(output);
})
.addErrback(function (e) {
sys.puts(e.stack);
});
1 change: 1 addition & 0 deletions examples/comments.html.mu
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>{{title}}{{! just something interesting... or not... }}</h1>
5 changes: 5 additions & 0 deletions examples/comments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
title: function() {
return "A Comedy of Errors";
}
}
1 change: 1 addition & 0 deletions examples/comments.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>A Comedy of Errors</h1>
16 changes: 16 additions & 0 deletions examples/complex.html.mu
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}}
19 changes: 19 additions & 0 deletions examples/complex.js
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;
}
}
6 changes: 6 additions & 0 deletions examples/complex.txt
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>
2 changes: 2 additions & 0 deletions examples/deep_partial.html.mu
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>First: {{title}}</h1>
{{>partial.html}}
3 changes: 3 additions & 0 deletions examples/deep_partial.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
title: "Welcome"
}
3 changes: 3 additions & 0 deletions examples/deep_partial.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<h1>First: Welcome</h1>
<h1>Welcome</h1>
Again, Welcome!
6 changes: 6 additions & 0 deletions examples/delimiters.html.mu
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{{=<% %>=}}* <% first %>
* <% second %>
<%=| |=%>
* | third |
|={{ }}=|
* {{ fourth }}
6 changes: 6 additions & 0 deletions examples/delimiters.js
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!."
}
6 changes: 6 additions & 0 deletions examples/delimiters.txt
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!.
1 change: 1 addition & 0 deletions examples/error_not_found.html.mu
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{foo}}
1 change: 1 addition & 0 deletions examples/error_not_found.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{bar: 2}
Empty file added examples/error_not_found.txt
Empty file.
1 change: 1 addition & 0 deletions examples/escaped.html.mu
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>{{title}}</h1>
5 changes: 5 additions & 0 deletions examples/escaped.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
title: function() {
return "Bear > Shark";
}
}
1 change: 1 addition & 0 deletions examples/escaped.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>Bear &gt; Shark</h1>
3 changes: 3 additions & 0 deletions examples/hash_instead_of_array.html.mu
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{#person}}
Name: {{name}}
{{/person}}
5 changes: 5 additions & 0 deletions examples/hash_instead_of_array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
person: {
name: "Chris"
}
}
2 changes: 2 additions & 0 deletions examples/hash_instead_of_array.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

Name: Chris
1 change: 1 addition & 0 deletions examples/inner_partial.html.mu
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Again, {{title}}!
2 changes: 2 additions & 0 deletions examples/partial.html.mu
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>{{title}}</h1>
{{>inner_partial.html}}
3 changes: 3 additions & 0 deletions examples/partial.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
title: "Welcome"
}
2 changes: 2 additions & 0 deletions examples/partial.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>Welcome</h1>
Again, Welcome!
7 changes: 7 additions & 0 deletions examples/recursion_with_same_names.html.mu
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{{ name }}
{{ description }}

{{#terms}}
{{name}}
{{index}}
{{/terms}}
8 changes: 8 additions & 0 deletions examples/recursion_with_same_names.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
name: 'name',
description: 'desc',
terms: [
{name: 't1', index: 0},
{name: 't2', index: 1},
]
}
7 changes: 7 additions & 0 deletions examples/recursion_with_same_names.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name
desc

t1
0
t2
1
8 changes: 8 additions & 0 deletions examples/reuse_of_enumerables.html.mu
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{{#terms}}
{{name}}
{{index}}
{{/terms}}
{{#terms}}
{{name}}
{{index}}
{{/terms}}
6 changes: 6 additions & 0 deletions examples/reuse_of_enumerables.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
terms: [
{name: 't1', index: 0},
{name: 't2', index: 1},
]
}
9 changes: 9 additions & 0 deletions examples/reuse_of_enumerables.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

t1
0
t2
1
t1
0
t2
1
5 changes: 5 additions & 0 deletions examples/simple.html.mu
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Hello {{name}}
You have just won ${{value}}!
{{#in_ca}}
Well, ${{ taxed_value }}, after taxes.
{{/in_ca}}
8 changes: 8 additions & 0 deletions examples/simple.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
name: "Chris",
value: 10000,
taxed_value: function() {
return this.value - (this.value * 0.4);
},
in_ca: true
}
3 changes: 3 additions & 0 deletions examples/simple.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Hello Chris
You have just won $10000!
Well, $6000, after taxes.
1 change: 1 addition & 0 deletions examples/two_in_a_row.html.mu
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{greeting}}, {{name}}!
4 changes: 4 additions & 0 deletions examples/two_in_a_row.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
name: "Joe",
greeting: "Welcome"
}
Loading

0 comments on commit 033e607

Please sign in to comment.