Express Remote Handlebars
Handlebars view engine for Express which transparently integrates remotely stored templates into your rendering flow.
This is useful in an SOA environment where separate applications are responsible for rendering different parts of a site while still requiring one shared layout.
Installation
Install using npm:
$ npm install --save express-remote-handlebars
Tests
Run tests using npm:
$ npm test
Usage
Simple App
A simple application will look as follows:
var express = ;var remoteHandlebars = ;var app = ; app;app; app;
In the example above, when res.render()
is called the view engine will:
- Get view and layout from cache if available - Stale items are revalidated in the background (configured using
stale-while-revalidate
) - Otherwise read
views/index.handlebars
from disk and downloadhttp://localhost/template.handlebars
- Cache local view forever and remote layout for 10 minutes (configured using
max-age
) - Render view and layout (view will be inserted in
{{{content}}}
placeholder which is configurable usingoptions.placeholder
) - Send response
Overriding Defaults
You can override instance specific defaults using the options param of res.render()
:
var express = ;var remoteHandlebars = ;var app = ; app;app; app; app;
See documentation for all overridable options.
Request in Parallel
In most applications you will get data from a database or REST API before rendering your views.
In order to minimize response times you can fetch layouts together with any other async request
using getLayout()
:
var express = ;var remoteHandlebars = ;var app = ; app;app; app;
Documentation
RemoteHandlebars (options)
Constructor to instantiate a new view engine.
Arguments
-
options.cacheControl
- Cache control string (default:max-age=60, stale-while-revalidate=600
)Use
max-age=<seconds>
to define when the item will expire.Combine with
stale-while-revalidate=<seconds>
to set the time window aftermax-age
has expired in which the item is marked as stale but still usable. After bothmax-age
andstale-while-revalidate
have expired the item will be deleted from cache.Examples:
no-cache, no-store, must-revalidate
- Will be dropped out of cache immediatelymax-age=600, must-revalidate
- Will be cached for 10 minutes and then dropped out of cachemax-age=600, stale-while-revalidate=86400
- Will be cached for 10 minutes and then revalidated in the background if the item is accessed again within a time window of 1 day
-
options.helpers
- Object with custom helper functions -
options.layout
- URL or request object of layout template (default: false) -
options.partialsDir
- Path(s) to partials (default: views/partials/) -
options.placeholder
- Name of content placeholder in layout (default: content) -
options.request (options, callback)
- Function used to request templates (Default: request) -
options.size
- Maximum number of layouts to cache (default: Infinity)
Examples
Simple:
var remoteHandlebars = ;app;
Factory:
var remoteHandlebars = ;app;
Constructor:
var RemoteHandlebars = RemoteHandlebars;app;
Note: Simple syntax only exposes the view engine. Factory and constructor pattern give access to public methods for more advanced use cases.
render (filePath, options, callback)
Called by Express when running res.render()
.
Arguments
filePath
- Path to templateoptions
- Context for template (Merged withapp.locals
andres.locals
)options.cache
- Toggle caching (This is set by Express viaapp.enable('view cache')
but can also be overridden manually)options.helpers
- Object with custom helper functionsoptions.layout
- URL, request object or template functionoptions.partialsDir
- Path(s) to partialscallback (error, rendered)
- Called once view with layout has been fully rendered
getLayout ([url, options], callback)
Fetches and compiles a template from remote.
Template is temporarily cached (see max-age
, stale-while-revalidate
and size
) unless disabled.
Arguments
url
- URL or request object of layout template (optional, default:this.layout
)options.cache
- Toggle caching (optional, default: true)callback (error, template)
- Called once template has been fetched and compiled
getView (filePath, [options], callback)
Reads and compiles a template from disk.
Template is cached forever unless disabled.
Arguments
filePath
- Path to templateoptions.cache
- Toggle caching (optional, default: true)callback (error, template)
- Called once template has been read and compiled
getPartials ([partialsDir, options], callback)
Recursively finds and compiles all partials in a directory.
Partials are cached forever unless disabled.
Arguments
partialsDir
- Path(s) to partials (optional, default:this.partialsDir
)options.cache
- Toggle caching (optional, default: true)callback (error, partials)
- Called once partials have been read and compiled