Serves mustache templates and partials, checking each directory in turn for matches, and reloading on file changes.
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-overlays
to get debug output from theexpress-mustache-overlays
library itself andexpress-mustache-overlays:server
for messages from the example server. Also includeexpress-public-files-overlays
to get debug from the public files server in the example.
Internally, the code is designed to work in these stages:
mustacheFromEnv(app)
- Parses and returns the config from theMUSTACHE_DIRS
environment variableprepareMustache(app, userDirs)
- Sets up theapp.locals.mustache
data structure with auserDirs
and alibDirs
key and makesapp.locals.mustache.overlay()
available (see next). TheuserDirs
argument is optional. You usually pass the output ofmustacheFromEnv(app)
as theuserDirs
variable. Any library directories (that should be used if a match can't be found in theuserDirs
) can be set up usingoverlay()
described next to add them intolibDirs
.app.locals.mustache.overlay(dirs)
- A function other libraries can use to merge any overlays they need into thelibDirs
configuration. TheuserDirs
configuration will always overlay over thelibDirs
configuration, 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.
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 (requiresawait
when called) which resolves to the path on the filesystem of the viewrenderView(template, options)
- async function (requiresawait
when called) which resolves to the template namedtemplate
, rendered withoptions
. E.g.const html = await renderView('content', {content: 'hello'})
renderFile(path, options)
- async function (requiresawait
when called) which resolves takes thepath
as the full path to the mustache template, and the sameoptions
asrenderView()
.
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 install
Then follow the instructions in the README.md
in the example
directory.
npm run fix
- Doc fixes
- Changed
prepareMustache = (app, userDirs, libDirs)
->prepareMustache = (app, userDirs)
- Added a warning if directories don't exist
- 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
- Changed debug behaviour to use own
debug()
, notapp.locals.debug()
. - Moved the example to
./example
.
- Complete refactor. See old
CHANGELOG.md
for older changes. Removed all functionality apart from the overlays behaviour. Seeexpress-public-files-overlays
for static file serving.