express-mustache-overlays v0.5.3
Express Mustache Overlays
Serves mustache templates and partials, checking each directory in turn for matches, and reloading on file changes.
Configuration
The components in this package make use of the app.locals.mustache namespace. The prepareMustache() function helps set up the data structure correctly.
Configuration environment variables for the example.
MUSTACHE_DIRS- A:-separated list of directories to check for templates e.g.mustache-overlay:mustache.
Any configuration from MUSTACHE_DIRS gets merged into existing configuration such that it is used in preference to it. Effectively, the MUSTACHE_DIRS settings override settings defined in code.
Additionally:
DEBUG- Includeexpress-mustache-overlaysto get debug output from theexpress-mustache-overlayslibrary itself andexpress-mustache-overlays:serverfor messages from the example server. Also includeexpress-public-files-overlaysto get debug from the public files server in the example.
Internal Workings
Internally, the code is designed to work in these stages:
mustacheFromEnv(app)- Parses and returns the config from theMUSTACHE_DIRSenvironment variableprepareMustache(app, userDirs)- Sets up theapp.locals.mustachedata structure with auserDirand alibDirsand makesapp.locals.mustache.overlay()available (see next).userDirsis optional. You usually pass the output ofmustacheFromEnv(app)as theuserDirsvariable. Any library directories (that should be used if a match can't be found in theuserDirs) can be set up usingoverlay()described next.app.locals.mustache.overlay(dirs)- A function other libraries can use to merge any overlays they need into thelibDirsconfiguration. TheuserDirsconfiguration will always overlay over thelibDirsconfiguration, even if it is set up earlier.setupMustache(app)- Installs the middleware based on the settings inapp.locals.mustache. This should always come last.
Internally a watch is set up using chokidar on any partials that are present
so that the updated contents can be used by the templates. Since templates (but
not partials) are read each time they are rendered, the watches are only added
to the partials since the latest template content will be rendered anyway.
Accessing the Overlays Object
You can access the overlays object like this once prepareMustache() is called:
app.locals.mustache.overlaysPromise.then((overlays) => {
// Use overlays here
})Ordinarily you wouldn't need this, but the object can be useful if you want to use the template system outside of Express.
The overlays object from the promise has these methods:
findView(template)- async function (requiresawaitwhen called) which resolves to the path on the filesystem of the viewrenderView(template, options)- async function (requiresawaitwhen called) which resolves to the template namedtemplate, rendered withoptions. E.g.const html = await renderView('content', {content: 'hello'})renderFile(path, options)- async function (requiresawaitwhen called) which resolves takes thepathas the full path to the mustache template, and the sameoptionsasrenderView().
Example
const express = require('express')
const path = require('path')
const { prepareMustache, setupMustache, mustacheFromEnv } = require('../index.js')
const app = express()
prepareMustache(app, mustacheFromEnv(app))
// Any other express setup can change app.locals.mustache.libDirs here to add
// additional library-defined public files directories to be served. Any user
// defined directories will be prepended before any corresponding URL path in
// the library directories list. The safest way to add overlays is with the
// overlay() function demonstrated here.
app.locals.mustache.overlay([path.join(__dirname, 'mustache')])
// Add any routes here:
app.get('', (req, res) => {
res.render('hello', {})
})
// Set up the engine
const mustacheEngine = setupMustache(app)
app.engine('mustache', mustacheEngine)
app.set('views', app.locals.mustache.dirs)
app.set('view engine', 'mustache')
app.listen(8000, () => console.log(`Example app listening on port 8000`))The mustache directory contains a hello.mustache template.
See the ./example directory for an example.
cd example
npm installThen follow the instructions in the README.md in the example directory.
Dev
npm run fixChangelog
0.5.3 2019-02-15
- Doc fixes
- Changed
prepareMustache = (app, userDirs, libDirs)->prepareMustache = (app, userDirs) - Added a warning if directories don't exist
0.5.2 2019-02-07
- Don't throw an error in the renderer, it creates an
UnhandledPromiseRejectionWarning. Calling the callback with an error is enough? - Improved the Docker example
- Improved logging
0.5.1 2019-02-07
- Changed debug behaviour to use own
debug(), notapp.locals.debug(). - Moved the example to
./example.
0.5.0 2019-02-06
- Complete refactor. See old
CHANGELOG.mdfor older changes. Removed all functionality apart from the overlays behaviour. Seeexpress-public-files-overlaysfor static file serving.
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago