mirror of
https://github.com/expressjs/expressjs.com.git
synced 2026-02-21 19:41:33 +00:00
42 lines
1.5 KiB
Plaintext
42 lines
1.5 KiB
Plaintext
section
|
|
h3(id='app.engine') app.engine(ext, callback)
|
|
|
|
p.
|
|
Register the given template engine <code>callback</code> as <code>ext</code>
|
|
|
|
By default will <code>require()</code> the engine based on the
|
|
file extension. For example if you try to render
|
|
a "foo.jade" file Express will invoke the following internally,
|
|
and cache the <code>require()</code> on subsequent calls to increase
|
|
performance.
|
|
|
|
+js.
|
|
app.engine('jade', require('jade').__express);
|
|
|
|
p.
|
|
For engines that do not provide <code>.__express</code> out of the box -
|
|
or if you wish to "map" a different extension to the template engine
|
|
you may use this method. For example mapping the EJS template engine to
|
|
".html" files:
|
|
|
|
+js.
|
|
app.engine('html', require('ejs').renderFile);
|
|
|
|
p.
|
|
In this case EJS provides a <code>.renderFile()</code> method with
|
|
the same signature that Express expects: <code>(path, options, callback)</code>,
|
|
though note that it aliases this method as <code>ejs.__express</code> internally
|
|
so if you're using ".ejs" extensions you dont need to do anything.
|
|
|
|
p.
|
|
Some template engines do not follow this convention, the
|
|
<a href="https://github.com/visionmedia/consolidate.js">consolidate.js</a>
|
|
library was created to map all of node's popular template
|
|
engines to follow this convention, thus allowing them to
|
|
work seemlessly within Express.
|
|
|
|
+js.
|
|
var engines = require('consolidate');
|
|
app.engine('haml', engines.haml);
|
|
app.engine('html', engines.hogan);
|