您的位置:首页 > Web前端 > Node.js

ahjesus让nodejs支持dotjs模板

2014-05-11 22:19 316 查看
经过几天的实验加搜索,终于知道一个中间件可以解决这个问题了

npm install consolidate


consolidate传送门 传送门2

使用说明传送门

快照:ahjesus

Since doT (and probably your template engine of choice, as well) is accessed through consolidate, consolidate is the only additional module I need to require at the top of
server.js
:

var express = require( "express" ),
app = express(),
cons = require( "consolidate" );

I want to continue serving some of my other pages statically, so I add my template configuration stuff below the existing
app.use
line in my code:

app.use( express.static( _dirname + "/public" ) );
app.engine( "dot", cons.dot );
app.set( "view engine", "dot" );
app.set( "views", _dirname + "/public/views" );

Those three new lines set doT (as exposed by consolidate) as the view engine, register files ending in
.dot
as templates, and tell Express to look in
/public/tmpl
for templates to use. So when Node sees
res.render( "detail", { ... } )
, it knows to expand
"detail"
to
/public/tmpl/detail.dot
and render it as a doT template. Now I can restart my server, go to http://localhost:3000/product/102, and see my template rendered statically, without creating a separate server-side file.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: