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 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
|
||||
-----
|
||||
|
||||
@@ -73,8 +73,10 @@ task :debug => [:compile, "dist/handlebars.debug.js"]
|
||||
desc "build the build, debug and base versions of handlebars"
|
||||
task :release => [:build, :debug, :base]
|
||||
|
||||
directory "vendor"
|
||||
|
||||
desc "benchmark against dust.js and mustache.js"
|
||||
task :bench do
|
||||
task :bench => "vendor" do
|
||||
require "open-uri"
|
||||
File.open("vendor/mustache.js", "w") do |file|
|
||||
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"
|
||||
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"
|
||||
end
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
var print = require("sys").print;
|
||||
|
||||
var Benchmark = require("benchmark");
|
||||
|
||||
var BenchWarmer = function(names) {
|
||||
@@ -8,6 +8,8 @@ var BenchWarmer = function(names) {
|
||||
this.errors = {};
|
||||
};
|
||||
|
||||
var print = require("sys").print;
|
||||
|
||||
BenchWarmer.prototype = {
|
||||
winners: function(benches) {
|
||||
var result = Benchmark.filter(benches, function(bench) { return bench.cycles; });
|
||||
@@ -56,8 +58,8 @@ BenchWarmer.prototype = {
|
||||
if(first) { self.startLine(suiteName); }
|
||||
self.writeBench(bench);
|
||||
self.currentBenches.push(bench);
|
||||
}, onError: function(bench) {
|
||||
self.errors[bench.name] = bench;
|
||||
}, onError: function() {
|
||||
self.errors[this.name] = this;
|
||||
}
|
||||
});
|
||||
|
||||
@@ -89,7 +91,7 @@ BenchWarmer.prototype = {
|
||||
print("\n" + new Array(horSize + 1).join("-"));
|
||||
|
||||
Benchmark.invoke(this.benchmarks, {
|
||||
methodName: "run",
|
||||
name: "run",
|
||||
onComplete: function() {
|
||||
var errors = false, prop, bench;
|
||||
for(prop in self.errors) { if(self.errors.hasOwnProperty(prop)) { errors = true; break; } }
|
||||
@@ -129,10 +131,9 @@ BenchWarmer.prototype = {
|
||||
|
||||
if(!bench.error) {
|
||||
var count = bench.hz,
|
||||
min = count - bench.MoE,
|
||||
max = count + bench.MoE;
|
||||
moe = count * bench.stats.RME / 100;
|
||||
|
||||
out = Math.round(count / 1000) + " ±" + Math.round(bench.MoE / 1000) + " (" + bench.cycles + ")";
|
||||
out = Math.round(count / 1000) + " ±" + Math.round(moe / 1000) + " (" + bench.cycles + ")";
|
||||
} else {
|
||||
out = "E";
|
||||
}
|
||||
|
||||
+43
-13
@@ -1,25 +1,38 @@
|
||||
require.paths.push("lib");
|
||||
require.paths.push("vendor");
|
||||
require.paths.push("vendor/dustjs/lib");
|
||||
require.paths.push("vendor/coffee/lib");
|
||||
require.paths.push("vendor/eco/lib");
|
||||
|
||||
|
||||
var BenchWarmer = require("./benchwarmer");
|
||||
Handlebars = require("handlebars").Handlebars;
|
||||
Handlebars = require("handlebars");
|
||||
|
||||
var dust = require("dust");
|
||||
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 = {
|
||||
string: {
|
||||
context: {},
|
||||
handlebars: "Hello world",
|
||||
dust: "Hello world",
|
||||
mustache: "Hello world"
|
||||
mustache: "Hello world",
|
||||
eco: "Hello world"
|
||||
},
|
||||
variables: {
|
||||
context: {name: "Mick", count: 30},
|
||||
handlebars: "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: {
|
||||
context: { person: { name: "Larry", age: 45 } },
|
||||
@@ -31,7 +44,8 @@ var benchDetails = {
|
||||
context: { names: [{name: "Moe"}, {name: "Larry"}, {name: "Curly"}, {name: "Shemp"}] },
|
||||
handlebars: "{{#each names}}{{name}}{{/each}}",
|
||||
dust: "{#names}{name}{/names}",
|
||||
mustache: "{{#names}}{{name}}{{/names}}"
|
||||
mustache: "{{#names}}{{name}}{{/names}}",
|
||||
eco: "<% for item in @names: %><%= item.name %><% end %>"
|
||||
},
|
||||
partial: {
|
||||
context: { peeps: [{name: "Moe", count: 15}, {name: "Larry", count: 5}, {name: "Curly", count: 1}] },
|
||||
@@ -88,6 +102,7 @@ var benchDetails = {
|
||||
};
|
||||
|
||||
handlebarsTemplates = {};
|
||||
ecoTemplates = {};
|
||||
|
||||
var warmer = new BenchWarmer();
|
||||
|
||||
@@ -99,26 +114,41 @@ var makeSuite = function(name) {
|
||||
var mustacheSource = details.mustache;
|
||||
var context = details.context;
|
||||
|
||||
bench("dust", function() {
|
||||
dust.render(templateName, context, function(err, out) { });
|
||||
});
|
||||
var error = function() { throw new Error("EWOT"); };
|
||||
|
||||
|
||||
//bench("dust", function() {
|
||||
//dust.render(templateName, context, function(err, out) { });
|
||||
//});
|
||||
|
||||
bench("handlebars", function() {
|
||||
handlebarsTemplates[templateName](context);
|
||||
});
|
||||
|
||||
if(mustacheSource) {
|
||||
bench("mustache", function() {
|
||||
Mustache.to_html(mustacheSource, context, mustachePartials);
|
||||
});
|
||||
}
|
||||
//if(ecoTemplates[templateName]) {
|
||||
//bench("eco", function() {
|
||||
//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) {
|
||||
if(benchDetails.hasOwnProperty(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;
|
||||
if(partials) {
|
||||
|
||||
@@ -345,7 +345,7 @@ Handlebars.JavaScriptCompiler = function() {};
|
||||
|
||||
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++) {
|
||||
params.push("depth" + this.environment.depths.list[i]);
|
||||
@@ -366,7 +366,7 @@ Handlebars.JavaScriptCompiler = function() {};
|
||||
|
||||
return function(context, helpers, partials, depth) {
|
||||
try {
|
||||
return container.render.apply(container, arguments);
|
||||
return container.render.call(container, Handlebars, context, helpers, partials, depth);
|
||||
} catch(e) {
|
||||
throw e;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
require "spec_helper"
|
||||
require "timeout"
|
||||
|
||||
describe "Tokenizer" do
|
||||
let(:parser) { @context["handlebars"] }
|
||||
|
||||
Reference in New Issue
Block a user