Files
zulip/static/js/templates.js
Steve Howell 2504baf783 Use compiled.js in dev environments.
Make our dev setup more similar to prod by using compiled.js,
instead of AJAX-ing templates on the fly and compiling them
with non-node code.  This will make our dev environment more
consistent with prod (to avoid surprising bugs), plus it should
be faster (fewer AJAX calls).

This change also means we don't have to keep two copies
of static/third/handlebars/handlebars.js around.

(imported from commit d8d584b9aa13adcdcce7e424033610d77d2df79b)
2014-01-10 21:39:04 -05:00

53 lines
1.6 KiB
JavaScript

var templates = (function () {
var exports = {};
exports.render = function (name, arg) {
if (Handlebars.templates === undefined) {
throw "Cannot find compiled templates!";
}
// The templates should be compiled into compiled.js. In
// prod we build compiled.js as part of the deployment process,
// and for devs we have run_dev.py build compiled.js when templates
// change.
return Handlebars.templates[name](arg);
};
// We don't want to wait for DOM ready to register the Handlebars helpers
// below. There's no need to, as they do not access the DOM.
// Furthermore, waiting for DOM ready would introduce race conditions with
// other DOM-ready callbacks that attempt to render templates.
// Regular Handlebars partials require pre-registering. This allows us
// to treat any template as a partial.
Handlebars.registerHelper('partial', function (template_name, context) {
return new Handlebars.SafeString(exports.render(template_name, this));
});
Handlebars.registerHelper('plural', function (condition, one, other) {
return (condition === 1) ? one : other;
});
Handlebars.registerHelper('if_and', function () {
// Execute the conditional code if all conditions are true.
// Example usage:
// {{#if_and cond1 cond2 cond3}}
// <p>All true</p>
// {{/if_and}}
var options = arguments[arguments.length - 1];
var i;
for (i = 0; i < arguments.length - 1; i++) {
if (!arguments[i]) {
return options.inverse(this);
}
}
return options.fn(this);
});
return exports;
}());
if (typeof module !== 'undefined') {
module.exports = templates;
}