2013-02-16 10:45:32 +01:00
|
|
|
var templates = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
|
|
|
exports.render = function (name, arg) {
|
2013-08-01 17:47:48 +02:00
|
|
|
if (Handlebars.templates === undefined) {
|
2013-02-16 10:45:32 +01:00
|
|
|
Handlebars.templates = {};
|
2013-08-01 17:47:48 +02:00
|
|
|
}
|
2013-02-16 10:45:32 +01:00
|
|
|
|
|
|
|
if (Handlebars.templates[name] === undefined) {
|
|
|
|
// Fetch the template using a synchronous AJAX request.
|
|
|
|
//
|
|
|
|
// This is only for local development. In prod we precompile
|
|
|
|
// templates and serve JavaScript which will have already
|
|
|
|
// populated Handlebars.templates.
|
|
|
|
$.ajax({
|
2013-04-04 17:20:25 +02:00
|
|
|
url: '/static/templates/'+name+'.handlebars?' + new Date().getTime(),
|
2013-02-16 10:45:32 +01:00
|
|
|
async: false,
|
|
|
|
success: function (data) {
|
|
|
|
Handlebars.templates[name] = Handlebars.compile(data);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return Handlebars.templates[name](arg);
|
|
|
|
};
|
|
|
|
|
2013-07-26 20:39:59 +02:00
|
|
|
$(function () {
|
|
|
|
// 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));
|
|
|
|
});
|
2013-07-25 22:08:16 +02:00
|
|
|
|
|
|
|
Handlebars.registerHelper('plural', function (condition, one, other) {
|
|
|
|
return (condition === 1) ? one : other;
|
|
|
|
});
|
2013-08-13 18:08:12 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
});
|
2013-07-26 20:39:59 +02:00
|
|
|
});
|
|
|
|
|
2013-02-16 10:45:32 +01:00
|
|
|
return exports;
|
|
|
|
}());
|