在Express中安装XTemplate
上一节讲了安装Express,并且生成了一个"microblog"的工程,我们的目标是在工程下安装XTemplate:
1.安装xtpl
npm install xtpl xtemplate --save
2.在views目录添加test.xtpl文件,其内容为
this is {{title}}!
3.可以做一个简单的测试,判断xtpl是否安装成功
var xtpl = require('xtpl');
xtpl.renderFile('./views/test.xtpl',{
title:'Express'
},function(error,content){
console.log(content);
});
输出:this is Express!
4.集成到Express中,只需要在app.js中,设置模板引擎即可
app.set('view engine', 'xtpl');
5.测试一个路径
var print = require('./routes/print');
app.use('/ooxx', print);
在print.js中
var express = require('express');
var router = express.Router();
router.get('/', function(req, res) {
res.render('test', { title: 'Express' });
});
module.exports = router;
6.此时如果在浏览器输入:127.0.0.1:3000/ooxx
显示为:this is Express!
需要注意的是,如果要自定义Express的模板引擎,是需要
Use the app.engine(ext, callback) method to create your own template engine. ext refers to the file extension, callbackis the template engine function which accepts the location of the file, the options object, and the callback function, as its parameters.
The following is an example of implementing a very simple template engine for rendering ".ntl" files.
var fs = require('fs'); // this engine requires the fs module
app.engine('ntl', function (filePath, options, callback) { // define the template engine
fs.readFile(filePath, function (err, content) {
if (err) throw new Error(err);
// this is an exteremly simple template engine
var rendered = content.toString().replace('#title#', '<title>'+ options.title +'</title>')
.replace('#message#', '<h1>'+ options.message +'</h1>');
return callback(null, rendered);
})
});
app.set('views', './views'); // specify the views directory
app.set('view engine', 'ntl'); // register the template engine
Your app will now be able to render ".ntl" files. Create a file named "index.ntl" in the views directory with the following content.
#title#
#message#
Then, create the following route in your app.
app.get('/', function (req, res) {
res.render('index', { title: 'Hey', message: 'Hello there!'});
})
其链接为:http://expressjs.com/advanced/developing-template-engines.html
也就是说要配置xptl的renderFile函数才行,不过为什么不用配置是可以的,后续还要看下
在Express中安装XTemplate的更多相关文章
- node+express 中安装nodemon实时更新server.js
每次启动node server.js,有一个缺点,每次server.js文件有改动时,必须重新执行指令node server.js,新的代码才会起作用 解决方案1 全局安装 npm install s ...
- 初学nodejs之安装Express中遇到的问题: error: option `-v, --view <engine>' argument missing
Windows安装下载nodejs地址:http://nodejs.org/download/ node -v 查看安装版本,输出版本即安装成功 之前学习了nodejs的基础,今天安装Express框 ...
- Nodejs express中创建ejs项目,解决express下默认创建jade,无法创建ejs问题
最近在看<Node.js开发指南>,看到使用nodejs进行web开发的时候,准备创建ejs项目遇到问题了, 书上命令为: express -t ejs microblog 可是执行后,仍 ...
- Windows下Node.js+Express+WebSocket 安装配置
Linux参考: Linux安装Node.js 使用Express搭建Web服务器 Node.js是一个Javascript运行环境(runtime).实际上它是对Google V8引擎进行了封装.V ...
- Access数据库导入到SQL Server 2005 Express中
安装好SQL Server 2005 Express后,再安装SQL Server Management Studio Express CTP就可以很方便的使用控制台进行数据库的管理.但SQL Ser ...
- Nodejs express中创建ejs项目 error install Couldn't read dependencies
最近在看<Node.js开发指南>,看到使用nodejs进行web开发的时候,准备创建ejs项目遇到问题了 书上命令为: express -t ejs microblog 可是执行 ...
- VC 2008 Express下安装OpenCV2.3.1
VC 2008 Express下安装OpenCV2.3.1 注意: 下列文档以VC2008 Express为例,VC2010下的配置应与本文档类似. VC 6.0不被OpenCV 2.3.1支持. ...
- node.js框架express的安装
node.js框架express的安装 首先假定你已经安装了 Node.js,接下来为你的应用创建一个目录,然后进入此目录并将其作为当前工作目录. $ mkdir myapp $ cd myapp 通 ...
- Node.js Express 的安装和简单使用
Express的安装: 1.命令行窗口 //--> npm install 组件名 @版本号 --> npm install express @4 //这里安装最新的版本 也可以这样: ...
随机推荐
- PAT1011
With the 2010 FIFA World Cup running, 随着2010世界杯的举行 football fans the world over were becoming increa ...
- 算法录 之 BFS和DFS
说一下BFS和DFS,这是个比较重要的概念,是很多很多算法的基础. 不过在说这个之前需要先说一下图和树,当然这里的图不是自拍的图片了,树也不是能结苹果的树了.这里要说的是图论和数学里面的概念. 以上概 ...
- Contaminated Milk
Contaminated Milk 题目描述 Farmer John, known far and wide for the quality of the milk produced on his f ...
- Sublime Text 3 搭建 Golang 开发环境
安装Golang go语言主页: https://golang.org/ go语言安装下载: https://golang.org/dl 环境变量设置: GOROOT: GOROOT变量设置go安装的 ...
- struts2-----新建项目
1. 建立界面原型 2. 建立Struts.xml 确定namespace, package, action, 空方法, result, 界面原型修改, 匹配现有设置, 测试, 做好规划 3. 建立数 ...
- RS485中继器电路(转)
源:RS485中继器电路(转) --------以上部分请勿修改!------------- RS-485中继器 由于在双绞线上的电平损耗,RS-485标准通信的最大传输距离是1200米(4000英尺 ...
- ios中操作技巧
1.配置字段快捷键: @property(nonatimic,copy) NSString *<#param#>; 2.NSNumber 转NSString 最快简单方式: NSNumbe ...
- Document Classification
Natural Language Processing with Python Chapter 6.1 由于nltk.FreqDist的排序问题,获取电影文本特征词的代码有些微改动. import n ...
- CORBA技术及实例
CORBA技术及实例 CORBA是一种规范,它定义了分布式对象如何实现互操作.在WorldWideWeb盛行之前,非凡是java编程语言风靡之前,C++开发者基本将CORBA作为其高端分布式对象的解决 ...
- CodeForces 652D Nested Segments
离散化+树状数组 先对坐标离散化,把每条线段结尾所在点标1, 询问某条线段内有几条线段的时候,只需询问这段区间的和是多少,询问结束之后再把这条线段尾部所在点标为0 #include<cstdio ...