88e7003331
* made it possible to define an alternate name lookup scheme (so that {{foo}} does not have to be
context.foo, but can instead be something like context.get('foo'))
* made it possible to substitute an alternate buffer instead of the default empty String and override how
the compiled template appends to the buffer
* Added the concept of template-local data. In order to enable template-local data, pass true
as the second parameter to the template compiler. Then, pass in the data as the fourth
parameter (context, helpers, partials, data). These signatures may change before the 1.0 release.
117 lines
2.6 KiB
JavaScript
117 lines
2.6 KiB
JavaScript
var handlebars = require("handlebars/parser").parser;
|
|
|
|
// BEGIN(BROWSER)
|
|
var Handlebars = {};
|
|
|
|
Handlebars.Parser = handlebars;
|
|
|
|
Handlebars.parse = function(string) {
|
|
Handlebars.Parser.yy = Handlebars.AST;
|
|
return Handlebars.Parser.parse(string);
|
|
};
|
|
|
|
Handlebars.print = function(ast) {
|
|
return new Handlebars.PrintVisitor().accept(ast);
|
|
};
|
|
|
|
Handlebars.Runtime = {};
|
|
|
|
Handlebars.Runtime.compile = function(string) {
|
|
var ast = Handlebars.parse(string);
|
|
|
|
return function(context, helpers, partials) {
|
|
helpers = helpers || Handlebars.helpers;
|
|
partials = partials || Handlebars.partials;
|
|
|
|
var internalContext = new Handlebars.Context(context, helpers, partials);
|
|
var runtime = new Handlebars.Runtime(internalContext);
|
|
runtime.accept(ast);
|
|
return runtime.buffer;
|
|
};
|
|
};
|
|
|
|
Handlebars.helpers = {};
|
|
Handlebars.partials = {};
|
|
|
|
Handlebars.registerHelper = function(name, fn, inverse) {
|
|
if(inverse) { fn.not = inverse; }
|
|
this.helpers[name] = fn;
|
|
};
|
|
|
|
Handlebars.registerPartial = function(name, str) {
|
|
this.partials[name] = str;
|
|
};
|
|
|
|
Handlebars.registerHelper('blockHelperMissing', function(context, fn, inverse) {
|
|
inverse = inverse || function() {};
|
|
|
|
var ret = "";
|
|
var type = Object.prototype.toString.call(context);
|
|
|
|
if(type === "[object Function]") {
|
|
context = context();
|
|
}
|
|
|
|
if(context === true) {
|
|
return fn(this);
|
|
} else if(context === false || context == null) {
|
|
return inverse(this);
|
|
} else if(type === "[object Array]") {
|
|
if(context.length > 0) {
|
|
for(var i=0, j=context.length; i<j; i++) {
|
|
ret = ret + fn(context[i]);
|
|
}
|
|
} else {
|
|
ret = inverse(this);
|
|
}
|
|
return ret;
|
|
} else {
|
|
return fn(context);
|
|
}
|
|
}, function(context, fn) {
|
|
return fn(context);
|
|
});
|
|
|
|
Handlebars.registerHelper('each', function(context, fn, inverse) {
|
|
var ret = "";
|
|
|
|
if(context && context.length > 0) {
|
|
for(var i=0, j=context.length; i<j; i++) {
|
|
ret = ret + fn(context[i]);
|
|
}
|
|
} else {
|
|
ret = inverse(this);
|
|
}
|
|
return ret;
|
|
});
|
|
|
|
Handlebars.registerHelper('if', function(context, fn, inverse) {
|
|
if(!context || context == []) {
|
|
return inverse(this);
|
|
} else {
|
|
return fn(this);
|
|
}
|
|
});
|
|
|
|
Handlebars.registerHelper('unless', function(context, fn, inverse) {
|
|
Handlebars.helpers['if'].call(this, context, inverse, fn);
|
|
});
|
|
|
|
Handlebars.registerHelper('with', function(context, fn) {
|
|
return fn(context);
|
|
});
|
|
|
|
Handlebars.logger = {
|
|
DEBUG: 0, INFO: 1, WARN: 2, ERROR: 3, level: 3,
|
|
|
|
// override in the host environment
|
|
log: function(level, str) {}
|
|
};
|
|
|
|
Handlebars.log = function(level, str) { Handlebars.logger.log(level, str); };
|
|
|
|
// END(BROWSER)
|
|
|
|
module.exports = Handlebars;
|
|
|