在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 //这里安装最新的版本 也可以这样: ...
随机推荐
- 图解SQL的各种连接join
对于SQL的Join,在学习起来可能是比较乱的.我们知道,SQL的Join语法有很多inner的,有outer的,有left的,有时候,对于Select出来的结果集是什么样子有点不是很清楚.Codin ...
- JAVA基础--继承和权限控制
1. extends继承 2. java只支持单继承,不允许多继承 修饰符 类内部 同一个包 子类 任何地方 private YES default YES YES protect ...
- wget获取https资源
使用wget获取 https资源,缺省命令下是要使用证书,如果还未安装证书,可以选择忽略. 例如没有github.com的证书,执行如下命令 mkdir -p model cd model wget ...
- Ubuntu iptables 设置
在ubuntu中由于不存在 /etc/init.d/iptales文件,所以无法使用service等命令来启动iptables,需要用modprobe命令. 启动iptables modprobe i ...
- Android Quick Tip - ADB over WiFi
http://stuffandtech.blogspot.jp/2012/03/android-quick-tip-adb-over-wifi.html MAR 26 Android Quick ...
- ZOJ 3946 Highway Project
1.迪杰斯特拉最小堆 #include<cstdio> #include<cstring> #include<cmath> #include<map> ...
- VS2010与SVN
http://blog.sina.com.cn/s/blog_4fe44775010182yl.html 在VS2010中使用SVN,必须先安装SVN的客户端,再安装VisualSVN(SVN的插件) ...
- cocopods安装与使用
转自http://www.cnblogs.com/jys509/p/4839803.html Cocoapods安装步骤 1.升级Ruby环境 sudo gem update --system 如果R ...
- SQL语句详细汇总
SQL语句详细汇总 | 浏览:3061 | 更新:2013-06-10 19:50 一.基础 1.说明:创建数据库 CREATE DATABASE database-name 2.说明:删除数据库 d ...
- HUST 1605 Gene recombination
简单广搜.4进制对应的10进制数来表示这些状态,总共只有(4^12)种状态. #include<cstdio> #include<cstring> #include<cm ...