Merge branch 'master' of github.com:wycats/handlebars.js
This commit is contained in:
+1
-1
@@ -5,7 +5,7 @@ Handlebars.js is an extension to the [Mustache templating language](http://musta
|
|||||||
|
|
||||||
Installing
|
Installing
|
||||||
----------
|
----------
|
||||||
Installing Handlebars is easy. Simply [download the package from GitHub](https://github.com/downloads/wycats/handlebars.js/handlebars.js) and add it to your web pages. You can also get a [debug version](https://github.com/downloads/wycats/handlebars.js/handlebars.debug.js) which helps newer browsers print out useful stack traces.
|
Installing Handlebars is easy. Simply [download the package from GitHub](https://github.com/wycats/handlebars.js/archives/master) and add it to your web pages (you should usually use the most recent version).
|
||||||
|
|
||||||
Usage
|
Usage
|
||||||
-----
|
-----
|
||||||
|
|||||||
@@ -73,8 +73,10 @@ task :debug => [:compile, "dist/handlebars.debug.js"]
|
|||||||
desc "build the build, debug and base versions of handlebars"
|
desc "build the build, debug and base versions of handlebars"
|
||||||
task :release => [:build, :debug, :base]
|
task :release => [:build, :debug, :base]
|
||||||
|
|
||||||
|
directory "vendor"
|
||||||
|
|
||||||
desc "benchmark against dust.js and mustache.js"
|
desc "benchmark against dust.js and mustache.js"
|
||||||
task :bench do
|
task :bench => "vendor" do
|
||||||
require "open-uri"
|
require "open-uri"
|
||||||
File.open("vendor/mustache.js", "w") do |file|
|
File.open("vendor/mustache.js", "w") do |file|
|
||||||
file.puts open("https://github.com/janl/mustache.js/raw/master/mustache.js").read
|
file.puts open("https://github.com/janl/mustache.js/raw/master/mustache.js").read
|
||||||
@@ -91,5 +93,17 @@ task :bench do
|
|||||||
system "git clone git://github.com/akdubya/dustjs.git vendor/dustjs"
|
system "git clone git://github.com/akdubya/dustjs.git vendor/dustjs"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if File.directory?("vendor/coffee")
|
||||||
|
system "cd vendor/coffee && git pull"
|
||||||
|
else
|
||||||
|
system "git clone git://github.com/jashkenas/coffee-script.git vendor/coffee"
|
||||||
|
end
|
||||||
|
|
||||||
|
if File.directory?("vendor/eco")
|
||||||
|
system "cd vendor/eco && git pull"
|
||||||
|
else
|
||||||
|
system "git clone git://github.com/sstephenson/eco.git vendor/eco"
|
||||||
|
end
|
||||||
|
|
||||||
system "node bench/handlebars.js"
|
system "node bench/handlebars.js"
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
var print = require("sys").print;
|
|
||||||
var Benchmark = require("benchmark");
|
var Benchmark = require("benchmark");
|
||||||
|
|
||||||
var BenchWarmer = function(names) {
|
var BenchWarmer = function(names) {
|
||||||
@@ -8,6 +8,8 @@ var BenchWarmer = function(names) {
|
|||||||
this.errors = {};
|
this.errors = {};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
var print = require("sys").print;
|
||||||
|
|
||||||
BenchWarmer.prototype = {
|
BenchWarmer.prototype = {
|
||||||
winners: function(benches) {
|
winners: function(benches) {
|
||||||
var result = Benchmark.filter(benches, function(bench) { return bench.cycles; });
|
var result = Benchmark.filter(benches, function(bench) { return bench.cycles; });
|
||||||
@@ -56,8 +58,8 @@ BenchWarmer.prototype = {
|
|||||||
if(first) { self.startLine(suiteName); }
|
if(first) { self.startLine(suiteName); }
|
||||||
self.writeBench(bench);
|
self.writeBench(bench);
|
||||||
self.currentBenches.push(bench);
|
self.currentBenches.push(bench);
|
||||||
}, onError: function(bench) {
|
}, onError: function() {
|
||||||
self.errors[bench.name] = bench;
|
self.errors[this.name] = this;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -89,7 +91,7 @@ BenchWarmer.prototype = {
|
|||||||
print("\n" + new Array(horSize + 1).join("-"));
|
print("\n" + new Array(horSize + 1).join("-"));
|
||||||
|
|
||||||
Benchmark.invoke(this.benchmarks, {
|
Benchmark.invoke(this.benchmarks, {
|
||||||
methodName: "run",
|
name: "run",
|
||||||
onComplete: function() {
|
onComplete: function() {
|
||||||
var errors = false, prop, bench;
|
var errors = false, prop, bench;
|
||||||
for(prop in self.errors) { if(self.errors.hasOwnProperty(prop)) { errors = true; break; } }
|
for(prop in self.errors) { if(self.errors.hasOwnProperty(prop)) { errors = true; break; } }
|
||||||
@@ -129,10 +131,9 @@ BenchWarmer.prototype = {
|
|||||||
|
|
||||||
if(!bench.error) {
|
if(!bench.error) {
|
||||||
var count = bench.hz,
|
var count = bench.hz,
|
||||||
min = count - bench.MoE,
|
moe = count * bench.stats.RME / 100;
|
||||||
max = count + bench.MoE;
|
|
||||||
|
|
||||||
out = Math.round(count / 1000) + " ±" + Math.round(bench.MoE / 1000) + " (" + bench.cycles + ")";
|
out = Math.round(count / 1000) + " ±" + Math.round(moe / 1000) + " (" + bench.cycles + ")";
|
||||||
} else {
|
} else {
|
||||||
out = "E";
|
out = "E";
|
||||||
}
|
}
|
||||||
|
|||||||
+43
-13
@@ -1,25 +1,38 @@
|
|||||||
require.paths.push("lib");
|
require.paths.push("lib");
|
||||||
require.paths.push("vendor");
|
require.paths.push("vendor");
|
||||||
require.paths.push("vendor/dustjs/lib");
|
require.paths.push("vendor/dustjs/lib");
|
||||||
|
require.paths.push("vendor/coffee/lib");
|
||||||
|
require.paths.push("vendor/eco/lib");
|
||||||
|
|
||||||
|
|
||||||
var BenchWarmer = require("./benchwarmer");
|
var BenchWarmer = require("./benchwarmer");
|
||||||
Handlebars = require("handlebars").Handlebars;
|
Handlebars = require("handlebars");
|
||||||
|
|
||||||
var dust = require("dust");
|
var dust = require("dust");
|
||||||
var Mustache = require("mustache");
|
var Mustache = require("mustache");
|
||||||
|
var ecoExports = require("eco");
|
||||||
|
|
||||||
|
eco = function(str) {
|
||||||
|
var module = {};
|
||||||
|
var template = new Function("module", ecoExports.compile(str));
|
||||||
|
template(module);
|
||||||
|
return module.exports;
|
||||||
|
}
|
||||||
|
|
||||||
var benchDetails = {
|
var benchDetails = {
|
||||||
string: {
|
string: {
|
||||||
context: {},
|
context: {},
|
||||||
handlebars: "Hello world",
|
handlebars: "Hello world",
|
||||||
dust: "Hello world",
|
dust: "Hello world",
|
||||||
mustache: "Hello world"
|
mustache: "Hello world",
|
||||||
|
eco: "Hello world"
|
||||||
},
|
},
|
||||||
variables: {
|
variables: {
|
||||||
context: {name: "Mick", count: 30},
|
context: {name: "Mick", count: 30},
|
||||||
handlebars: "Hello {{name}}! You have {{count}} new messages.",
|
handlebars: "Hello {{name}}! You have {{count}} new messages.",
|
||||||
dust: "Hello {name}! You have {count} new messages.",
|
dust: "Hello {name}! You have {count} new messages.",
|
||||||
mustache: "Hello {{name}}! You have {{count}} new messages."
|
mustache: "Hello {{name}}! You have {{count}} new messages.",
|
||||||
|
eco: "Hello <%= @name %>! You have <%= @count %> new messages."
|
||||||
},
|
},
|
||||||
object: {
|
object: {
|
||||||
context: { person: { name: "Larry", age: 45 } },
|
context: { person: { name: "Larry", age: 45 } },
|
||||||
@@ -31,7 +44,8 @@ var benchDetails = {
|
|||||||
context: { names: [{name: "Moe"}, {name: "Larry"}, {name: "Curly"}, {name: "Shemp"}] },
|
context: { names: [{name: "Moe"}, {name: "Larry"}, {name: "Curly"}, {name: "Shemp"}] },
|
||||||
handlebars: "{{#each names}}{{name}}{{/each}}",
|
handlebars: "{{#each names}}{{name}}{{/each}}",
|
||||||
dust: "{#names}{name}{/names}",
|
dust: "{#names}{name}{/names}",
|
||||||
mustache: "{{#names}}{{name}}{{/names}}"
|
mustache: "{{#names}}{{name}}{{/names}}",
|
||||||
|
eco: "<% for item in @names: %><%= item.name %><% end %>"
|
||||||
},
|
},
|
||||||
partial: {
|
partial: {
|
||||||
context: { peeps: [{name: "Moe", count: 15}, {name: "Larry", count: 5}, {name: "Curly", count: 1}] },
|
context: { peeps: [{name: "Moe", count: 15}, {name: "Larry", count: 5}, {name: "Curly", count: 1}] },
|
||||||
@@ -88,6 +102,7 @@ var benchDetails = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
handlebarsTemplates = {};
|
handlebarsTemplates = {};
|
||||||
|
ecoTemplates = {};
|
||||||
|
|
||||||
var warmer = new BenchWarmer();
|
var warmer = new BenchWarmer();
|
||||||
|
|
||||||
@@ -99,26 +114,41 @@ var makeSuite = function(name) {
|
|||||||
var mustacheSource = details.mustache;
|
var mustacheSource = details.mustache;
|
||||||
var context = details.context;
|
var context = details.context;
|
||||||
|
|
||||||
bench("dust", function() {
|
var error = function() { throw new Error("EWOT"); };
|
||||||
dust.render(templateName, context, function(err, out) { });
|
|
||||||
});
|
|
||||||
|
//bench("dust", function() {
|
||||||
|
//dust.render(templateName, context, function(err, out) { });
|
||||||
|
//});
|
||||||
|
|
||||||
bench("handlebars", function() {
|
bench("handlebars", function() {
|
||||||
handlebarsTemplates[templateName](context);
|
handlebarsTemplates[templateName](context);
|
||||||
});
|
});
|
||||||
|
|
||||||
if(mustacheSource) {
|
//if(ecoTemplates[templateName]) {
|
||||||
bench("mustache", function() {
|
//bench("eco", function() {
|
||||||
Mustache.to_html(mustacheSource, context, mustachePartials);
|
//ecoTemplates[templateName](context);
|
||||||
});
|
//});
|
||||||
}
|
//} else {
|
||||||
|
//bench("eco", error);
|
||||||
|
//}
|
||||||
|
|
||||||
|
//if(mustacheSource) {
|
||||||
|
//bench("mustache", function() {
|
||||||
|
//Mustache.to_html(mustacheSource, context, mustachePartials);
|
||||||
|
//});
|
||||||
|
//} else {
|
||||||
|
//bench("mustache", error);
|
||||||
|
//}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
for(var name in benchDetails) {
|
for(var name in benchDetails) {
|
||||||
if(benchDetails.hasOwnProperty(name)) {
|
if(benchDetails.hasOwnProperty(name)) {
|
||||||
dust.loadSource(dust.compile(benchDetails[name].dust, name));
|
dust.loadSource(dust.compile(benchDetails[name].dust, name));
|
||||||
handlebarsTemplates[name] = Handlebars.VM.compile(benchDetails[name].handlebars);
|
handlebarsTemplates[name] = Handlebars.compile(benchDetails[name].handlebars);
|
||||||
|
|
||||||
|
if(benchDetails[name].eco) { ecoTemplates[name] = eco(benchDetails[name].eco); }
|
||||||
|
|
||||||
var partials = benchDetails[name].partials;
|
var partials = benchDetails[name].partials;
|
||||||
if(partials) {
|
if(partials) {
|
||||||
|
|||||||
@@ -345,7 +345,7 @@ Handlebars.JavaScriptCompiler = function() {};
|
|||||||
|
|
||||||
this.source.push("return buffer;");
|
this.source.push("return buffer;");
|
||||||
|
|
||||||
var params = ["context", "helpers", "partials"];
|
var params = ["Handlebars", "context", "helpers", "partials"];
|
||||||
|
|
||||||
for(var i=0, l=this.environment.depths.list.length; i<l; i++) {
|
for(var i=0, l=this.environment.depths.list.length; i<l; i++) {
|
||||||
params.push("depth" + this.environment.depths.list[i]);
|
params.push("depth" + this.environment.depths.list[i]);
|
||||||
@@ -366,7 +366,7 @@ Handlebars.JavaScriptCompiler = function() {};
|
|||||||
|
|
||||||
return function(context, helpers, partials, depth) {
|
return function(context, helpers, partials, depth) {
|
||||||
try {
|
try {
|
||||||
return container.render.apply(container, arguments);
|
return container.render.call(container, Handlebars, context, helpers, partials, depth);
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
require "spec_helper"
|
require "spec_helper"
|
||||||
|
require "timeout"
|
||||||
|
|
||||||
describe "Tokenizer" do
|
describe "Tokenizer" do
|
||||||
let(:parser) { @context["handlebars"] }
|
let(:parser) { @context["handlebars"] }
|
||||||
|
|||||||
Reference in New Issue
Block a user