diff options
author | Dave Arter <davea@mysociety.org> | 2015-08-27 15:25:15 +0100 |
---|---|---|
committer | Dave Arter <davea@mysociety.org> | 2015-08-28 14:27:16 +0100 |
commit | 3331192bb306dd1986477185db787f4c0ed87aef (patch) | |
tree | 8efe46e87217862836b418e82ab0df8dd79c3cee | |
parent | c2097548f58db2efe96dc5442d85526000176393 (diff) |
Allow cobrands to override templates
-rw-r--r-- | www/js/app.js | 49 |
1 files changed, 40 insertions, 9 deletions
diff --git a/www/js/app.js b/www/js/app.js index 40633d6..a3c85be 100644 --- a/www/js/app.js +++ b/www/js/app.js @@ -13,15 +13,46 @@ var tpl = { var loadTemplate = function (index) { var name = names[index]; FMS.printDebug('Loading template: ' + name + ', index: ' + index); - $.get('templates/' + CONFIG.LANGUAGE + '/' + name + '.html', function (data) { - that.templates[name] = data; - index++; - if (index < names.length) { - loadTemplate(index); - } else { - callback(); - } - }); + // Build a list of possible paths to the template, which will be tried in order. + var template_paths = [ + 'templates/' + CONFIG.LANGUAGE + '/' + name + '.html' + ]; + // cobrands can override the base templates by creating the appropriate file in + // /www/cobrands/<cobrand name>/templates/<language>, so put the cobranded template + // path first in the list if we're using a cobrand. + if (CONFIG.COBRAND) { + template_paths.unshift('cobrands/' + CONFIG.COBRAND + '/templates/' + CONFIG.LANGUAGE + '/' + name + '.html'); + } + // Fetch a template by loading it over AJAX. + // Returns a jqXHR object so failures can be handed elsewhere + var getTemplate = function(template_path) { + FMS.printDebug("Fetching template: " + template_path); + return $.get(template_path, function (data) { + FMS.printDebug("success!") + that.templates[name] = data; + index++; + if (index < names.length) { + loadTemplate(index); + } else { + callback(); + } + }); + }; + + // Try to get the first template in template_paths, working down the list + // in case of failure. + var tryToGetTemplate = function() { + getTemplate(template_paths[0]) + .fail(function() { + template_paths.shift(); + if (template_paths.length) { + tryToGetTemplate(); + } + }); + } + + // Kick-off template loading for this template. + tryToGetTemplate(); }; loadTemplate(0); |