059a80661d
* Unify inverse and normal block helpers * Make Handlebars.Exception inherit from JS Error
115 lines
2.6 KiB
JavaScript
115 lines
2.6 KiB
JavaScript
var handlebars = require("handlebars/parser").parser;
|
|
|
|
// BEGIN(BROWSER)
|
|
var Handlebars = {};
|
|
|
|
Handlebars.VERSION = "1.0.beta.2";
|
|
|
|
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.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('helperMissing', function(arg) {
|
|
if(arguments.length === 2) {
|
|
return undefined;
|
|
} else {
|
|
throw new Error("Could not find property '" + arg + "'");
|
|
}
|
|
});
|
|
|
|
Handlebars.registerHelper('blockHelperMissing', function(context, options) {
|
|
var inverse = options.inverse || function() {}, fn = options.fn;
|
|
|
|
|
|
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);
|
|
}
|
|
});
|
|
|
|
Handlebars.registerHelper('each', function(context, options) {
|
|
var fn = options.fn, inverse = options.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, options) {
|
|
if(!context || Handlebars.Utils.isEmpty(context)) {
|
|
return options.inverse(this);
|
|
} else {
|
|
return options.fn(this);
|
|
}
|
|
});
|
|
|
|
Handlebars.registerHelper('unless', function(context, options) {
|
|
var fn = options.fn, inverse = options.inverse;
|
|
options.fn = inverse;
|
|
options.inverse = fn;
|
|
|
|
return Handlebars.helpers['if'].call(this, context, options);
|
|
});
|
|
|
|
Handlebars.registerHelper('with', function(context, options) {
|
|
return options.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;
|
|
|