首页 > 网站 > WEB开发 > 正文

ahjesus让nodejs支持dotjs模板

2024-04-27 14:24:14
字体:
来源:转载
供稿:网友

ahjesus让nodejs支持dotjs模板

经过几天的实验加搜索,终于知道一个中间件可以解决这个问题了

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 ofserver.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 existingapp.useline 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.dotas templates, and tell Express to look in/public/tmplfor templates to use. So when Node seesres.render( "detail", { ... } ), it knows to expand"detail"to/public/tmpl/detail.dotand 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.


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表