hogan-middleware
Advanced tools
Comparing version 0.1.0 to 0.2.1
{ | ||
"name": "hogan-middleware", | ||
"version": "0.1.0", | ||
"version": "0.2.1", | ||
"description": "Middleware component to use Hogan.js mustache templates as views in an Express server", | ||
@@ -5,0 +5,0 @@ "author": "Steve King <steve@mydev.co>", |
@@ -32,3 +32,25 @@ hogan-middleware | ||
</html> | ||
Configuration | ||
============= | ||
Optional functionality in the middleware can be set before passing it into express: | ||
``` | ||
var hoganMiddleware = require('hogan-middleware'); | ||
hoganMiddleware({ | ||
filter: ['**.mustache'], // override the default file extension searched for | ||
// default is just the mustache file extension | ||
flatten: true, // make all partials available with just their file name | ||
// rather than the slash delimited path. default is enabled | ||
watch: true // set to false to remove the live updating watchers - | ||
// can be useful for running in production where files | ||
// will not be regularly changing. | ||
}); | ||
app.engine('mustache', hoganMiddleware.__express); | ||
``` | ||
Partial Templates | ||
@@ -47,3 +69,6 @@ ================= | ||
Note - multiple templates with the same name but in different directories will overwrite each other. | ||
Note - multiple templates with the same name but in different directories will overwrite each other. Set the | ||
`flatten` configuration option to false to always use the relative path as the name of the partials | ||
(ie: `{{>app/header}}` instead of just `{{>header}}`). Whether the `flatten` option is enabled or not, the relative | ||
path name will always be available. | ||
@@ -50,0 +75,0 @@ Note - don't include the same template as a partial inside itself. |
@@ -23,3 +23,5 @@ | ||
TemplateEngine.__settings = { | ||
filter: ['**.mustache'] | ||
filter: ['**.mustache'], | ||
flatten: true, | ||
watch: true | ||
}; | ||
@@ -63,15 +65,2 @@ | ||
/** | ||
* Stores an individual template based on the supplied path, the name of the template is the file's basename without | ||
* the extension. | ||
* | ||
* @param {String} templatePath | ||
*/ | ||
TemplateEngine._storeTemplate = function(templatePath) { | ||
var templateName = Path.basename(templatePath, Path.extname(templatePath)); | ||
TemplateEngine.__templates[templateName] = Hogan.compile(FS.readFileSync(templatePath, 'utf-8')); | ||
debug('Stored template %s', templateName); | ||
}; | ||
/** | ||
* Gets all templates, when the template path hasn't yet been scanned it will be read synchronously to ensure there are | ||
@@ -86,3 +75,11 @@ * always templates available, the template directory is then watched to allow templates to be changed while the server | ||
TemplateEngine._refreshTemplates(templatesPath); | ||
FS.watch(templatesPath, {persistent: false}, TemplateEngine._refreshTemplates.bind(TemplateEngine, templatesPath)); | ||
FS.watch( | ||
templatesPath, | ||
{ | ||
persistent: false | ||
}, | ||
function refreshTemplates (a,b,c) { | ||
return TemplateEngine._refreshTemplates(templatesPath); | ||
} | ||
); | ||
} | ||
@@ -97,2 +94,7 @@ return TemplateEngine.__templates; | ||
TemplateEngine._refreshWatches = function(templatesPath) { | ||
if (TemplateEngine.__settings.watch === false) { | ||
debug('Refreshing watched directories has been disabled.'); | ||
return; | ||
} | ||
debug('Refreshing watched directories'); | ||
@@ -118,15 +120,44 @@ | ||
* | ||
* @param {String} templatesPath | ||
* @param {String} templateRootPath | ||
*/ | ||
TemplateEngine._refreshTemplates = function(templatesPath) { | ||
debug('Refreshing templates for %s', templatesPath); | ||
TemplateEngine._refreshTemplates = function(templateRootPath) { | ||
debug('Refreshing templates for %s', templateRootPath); | ||
TemplateEngine._refreshWatches(templatesPath); | ||
TemplateEngine._refreshWatches(templateRootPath); | ||
TemplateEngine.__templates = {}; | ||
ReadDir.readSync(templatesPath, TemplateEngine.__settings.filter, ReadDir.ABSOLUTE_PATHS) | ||
.forEach(TemplateEngine._storeTemplate, TemplateEngine); | ||
var settings = TemplateEngine.__settings; | ||
findTemplates(templateRootPath, settings.filter) | ||
.map(readTemplate) | ||
.reduce(function (templates, template) { | ||
if (settings.flatten) { | ||
templates[Path.basename(template.path)] = template.content; | ||
} | ||
templates[Path.relative(templateRootPath, template.path)] = template.content; | ||
return templates; | ||
}, TemplateEngine.__templates = {}); | ||
debug('Refreshing templates complete'); | ||
}; | ||
function findTemplates (rootPath, filter) { | ||
return ReadDir.readSync(rootPath, filter, ReadDir.ABSOLUTE_PATHS); | ||
} | ||
function readTemplate (absolutePath) { | ||
return { | ||
content: Hogan.compile(FS.readFileSync(absolutePath, 'utf-8')), | ||
path: stripFileExtension(absolutePath) | ||
}; | ||
} | ||
function stripFileExtension (input) { | ||
return input.replace(/(.+)\.[a-z]+/, '$1'); | ||
} | ||
module.exports = TemplateEngine; |
8526
133
81