2013-02-16 10:45:32 +01:00
|
|
|
var templates = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
|
|
|
exports.render = function (name, arg) {
|
2016-03-07 03:51:10 +01:00
|
|
|
if (Handlebars.templates === undefined) {
|
2016-10-28 07:01:45 +02:00
|
|
|
throw new Error("Cannot find compiled templates!");
|
2013-02-16 10:45:32 +01:00
|
|
|
}
|
2016-07-21 00:56:27 +02:00
|
|
|
if (Handlebars.templates[name] === undefined) {
|
2016-10-28 07:01:45 +02:00
|
|
|
throw new Error("Cannot find a template with this name: " + name
|
2017-06-22 23:40:41 +02:00
|
|
|
+ ". If you are developing a new feature, this likely "
|
2016-07-21 00:56:27 +02:00
|
|
|
+ "means you need to add the file static/templates/"
|
2016-10-28 07:01:45 +02:00
|
|
|
+ name + ".handlebars");
|
2016-07-21 00:56:27 +02:00
|
|
|
}
|
2013-02-16 10:45:32 +01:00
|
|
|
|
2014-01-09 23:51:32 +01:00
|
|
|
// 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.
|
2013-02-16 10:45:32 +01:00
|
|
|
return Handlebars.templates[name](arg);
|
|
|
|
};
|
|
|
|
|
2013-08-27 20:17:42 +02:00
|
|
|
// 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.
|
|
|
|
|
2014-02-26 00:16:17 +01:00
|
|
|
// Regular Handlebars partials require pre-registering. This allows us to treat
|
|
|
|
// any template as a partial. We also allow the partial to be passed additional
|
|
|
|
// named arguments. Arguments should alternate between strings which will be
|
|
|
|
// used as the name and the associated value.
|
|
|
|
Handlebars.registerHelper('partial', function (template_name) {
|
|
|
|
var extra_data = {};
|
|
|
|
var args_len = arguments.length;
|
|
|
|
var i;
|
|
|
|
|
|
|
|
for (i = 1; i < args_len - 2; i += 2) {
|
|
|
|
extra_data[arguments[i]] = arguments[i + 1];
|
|
|
|
}
|
|
|
|
var data = _.extend({}, this, extra_data);
|
|
|
|
|
|
|
|
return new Handlebars.SafeString(exports.render(template_name, data));
|
2013-08-27 20:17:42 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
Handlebars.registerHelper('plural', function (condition, one, other) {
|
2018-06-06 18:19:09 +02:00
|
|
|
return condition === 1 ? one : other;
|
2013-08-27 20:17:42 +02:00
|
|
|
});
|
|
|
|
|
2019-06-25 02:54:18 +02:00
|
|
|
Handlebars.registerHelper({
|
|
|
|
eq: function (a, b) { return a === b; },
|
|
|
|
and: function () {
|
|
|
|
// last argument is Handlebars options
|
|
|
|
if (arguments.length < 2) {
|
|
|
|
return true;
|
2013-08-13 18:08:12 +02:00
|
|
|
}
|
2019-06-25 02:54:18 +02:00
|
|
|
var i;
|
|
|
|
for (i = 0; i < arguments.length - 2; i += 1) {
|
|
|
|
if (!arguments[i] || Handlebars.Utils.isEmpty(arguments[i])) {
|
|
|
|
return arguments[i];
|
|
|
|
}
|
2014-02-13 21:16:43 +01:00
|
|
|
}
|
2019-06-25 02:54:18 +02:00
|
|
|
return arguments[i];
|
|
|
|
},
|
|
|
|
or: function () {
|
|
|
|
// last argument is Handlebars options
|
|
|
|
if (arguments.length < 2) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
var i;
|
|
|
|
for (i = 0; i < arguments.length - 2; i += 1) {
|
|
|
|
if (arguments[i] && !Handlebars.Utils.isEmpty(arguments[i])) {
|
|
|
|
return arguments[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return arguments[i];
|
|
|
|
},
|
|
|
|
not: function (a) { return !a || Handlebars.Utils.isEmpty(a); },
|
2018-09-02 09:38:09 +02:00
|
|
|
});
|
|
|
|
|
2016-05-13 12:44:03 +02:00
|
|
|
Handlebars.registerHelper('t', function (i18n_key) {
|
|
|
|
// Marks a string for translation.
|
|
|
|
// Example usage:
|
|
|
|
// {{t "some English text"}}
|
|
|
|
var result = i18n.t(i18n_key);
|
|
|
|
return new Handlebars.SafeString(result);
|
|
|
|
});
|
|
|
|
|
|
|
|
Handlebars.registerHelper('tr', function (context, options) {
|
|
|
|
// Marks a block for translation.
|
|
|
|
// Example usage 1:
|
|
|
|
// {{#tr context}}
|
|
|
|
// <p>some English text</p>
|
|
|
|
// {{/tr}}
|
|
|
|
//
|
|
|
|
// Example usage 2:
|
|
|
|
// {{#tr context}}
|
|
|
|
// <p>This __variable__ will get value from context</p>
|
|
|
|
// {{/tr}}
|
|
|
|
//
|
|
|
|
// Notes:
|
|
|
|
// 1. `context` is very important. It can be `this` or an
|
|
|
|
// object or key of the current context.
|
|
|
|
// 2. Use `__` instead of `{{` and `}}` to declare expressions
|
|
|
|
var result = i18n.t(options.fn(context), context);
|
|
|
|
return new Handlebars.SafeString(result);
|
|
|
|
});
|
|
|
|
|
2013-02-16 10:45:32 +01:00
|
|
|
return exports;
|
|
|
|
}());
|
2013-11-26 16:39:58 +01:00
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = templates;
|
|
|
|
}
|
2018-05-28 08:04:36 +02:00
|
|
|
window.templates = templates;
|