templar
A minimal template thing for node.js web sites to use.
Works with any template engine that works with Express.
Automatically sends ETags based on the data and the template being used,
and 304 responses based on the If-None-Match
request header, if the
user would be getting the same exact response as last time.
Example
var ejs = Templar = templarOptions = engine: ejs folder: './templates' // preload it. Otherwise, the first request is slow, because// it has to load up all the templates within it.Templar http
Options
engine
: The engine to use. EJS and Jade both work.folder
: The folder where template files are found.cache
: Boolean. Set tofalse
to suppress 304 responses and re-read templates without restarting the process.
Partials
Every template will be provided with a local function
include(file, data)
. This function will include another template via
a relative path, run it using the data provided, and return the string.
Note that this does not automatically dump the data into the calling template! It's still the caller's responsibility to actually print out the result.
Example
If the template full.ejs
contains this:
yoyoyoyo<%- include("partial.ejs", { partial: 1 }) %><%- include("partial.ejs", { partial: 2 }) %><%- include("partial.ejs", { partial: 3 }) %><%- include("partial.ejs", { partial: 4 }) %><%- include("partial.ejs", { partial: 5 }) %>
Then, in the same folder, you had a partial.ejs
that contained:
<p>is for <%= partial %>
then the resulting output would be:
yoyoyoyois for 1 is for 2 is for 3 is for 4 is for 5
Note that full.ejs
actually prints out the result of the include call.