Skip to content
This repository has been archived by the owner on Jun 1, 2021. It is now read-only.

Commit

Permalink
Generalize template-to-json mapping to configuration items
Browse files Browse the repository at this point in the history
  • Loading branch information
joelpurra committed Feb 9, 2014
1 parent ffb9d72 commit 6f90ca6
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions src/render-templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ var configuration = require("configvention"),
getTemplate(templateFilename, function(error, template) {
failOrNot(error);

doT.templateSettings.useParams = /(^|[^\w$])def(?:\.|\[[\'\"])([\w$\.]+)(?:[\'\"]\])?\s*\:\s*((?:(?:[\w$]+|\[(?:\"[^\"]+\"|\'[^\']+\')\]|\{[^\}]+\})\.?)*)/g;

var compiledTemplate = doT.template(template),
output = compiledTemplate(input);

Expand All @@ -83,22 +85,37 @@ var configuration = require("configvention"),
});
},

renderTemplatesToHtml = function(erroneousVotes) {
renderTemplateToHtml(erroneousVotes, "index.html");
renderTemplatesToHtml = function(json, templateFilename) {
renderTemplateToHtml(json, templateFilename);
},

loadJson = function(path) {
var resolved = resolvePath(path),
json = require(path);
json = require(resolved);

return json
return json;
},

init = function() {
var erroneousVotesPath = configuration.get("erroneous-votes") || fail(null, "erroneous-votes was not defined"),
erroneousVotes = loadJson(erroneousVotesPath);
renderFromConfiguration = function(templateFilename, templateConfiguration) {
var dataConfigurationName = templateConfiguration.data,
path = configuration.get(dataConfigurationName) || fail(null, "The configuration for " + dataConfigurationName + " was not defined."),
json = loadJson(path);

renderTemplatesToHtml(json, templateFilename);
},

renderFromConfigurations = function() {
var templatesConfiguration = configuration.get("templates");

renderTemplatesToHtml(erroneousVotes);
Object.keys(templatesConfiguration).forEach(function(templateFilename, index) {
var templateConfiguration = templatesConfiguration[templateFilename];

renderFromConfiguration(templateFilename, templateConfiguration);
});
},

init = function() {
renderFromConfigurations();
};

init();

0 comments on commit 6f90ca6

Please sign in to comment.