上一节讲了安装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的更多相关文章

  1. node+express 中安装nodemon实时更新server.js

    每次启动node server.js,有一个缺点,每次server.js文件有改动时,必须重新执行指令node server.js,新的代码才会起作用 解决方案1 全局安装 npm install s ...

  2. 初学nodejs之安装Express中遇到的问题: error: option `-v, --view <engine>' argument missing

    Windows安装下载nodejs地址:http://nodejs.org/download/ node -v 查看安装版本,输出版本即安装成功 之前学习了nodejs的基础,今天安装Express框 ...

  3. Nodejs express中创建ejs项目,解决express下默认创建jade,无法创建ejs问题

    最近在看<Node.js开发指南>,看到使用nodejs进行web开发的时候,准备创建ejs项目遇到问题了, 书上命令为: express -t ejs microblog 可是执行后,仍 ...

  4. Windows下Node.js+Express+WebSocket 安装配置

    Linux参考: Linux安装Node.js 使用Express搭建Web服务器 Node.js是一个Javascript运行环境(runtime).实际上它是对Google V8引擎进行了封装.V ...

  5. Access数据库导入到SQL Server 2005 Express中

    安装好SQL Server 2005 Express后,再安装SQL Server Management Studio Express CTP就可以很方便的使用控制台进行数据库的管理.但SQL Ser ...

  6. Nodejs express中创建ejs项目 error install Couldn't read dependencies

    最近在看<Node.js开发指南>,看到使用nodejs进行web开发的时候,准备创建ejs项目遇到问题了   书上命令为:   express -t ejs microblog 可是执行 ...

  7. VC 2008 Express下安装OpenCV2.3.1

    VC 2008 Express下安装OpenCV2.3.1   注意: 下列文档以VC2008 Express为例,VC2010下的配置应与本文档类似. VC 6.0不被OpenCV 2.3.1支持. ...

  8. node.js框架express的安装

    node.js框架express的安装 首先假定你已经安装了 Node.js,接下来为你的应用创建一个目录,然后进入此目录并将其作为当前工作目录. $ mkdir myapp $ cd myapp 通 ...

  9. Node.js Express 的安装和简单使用

    Express的安装: 1.命令行窗口 //--> npm install 组件名 @版本号 --> npm install express @4   //这里安装最新的版本 也可以这样: ...

随机推荐

  1. 64位系统访问注册表SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall

    public int ChecNonkWoW64() { try { ; string subKey = @"SOFTWARE\Microsoft\Windows\CurrentVersio ...

  2. DataTable的名称要后设置

    string sqldatabase = string.Format(dr["sql"].ToString(), drpat["PATIENT_ID"].ToS ...

  3. Github for Windows使用图文教程_西西软件资讯

    body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...

  4. Inside dependency property

    依赖属性的定义,分为3步(以PresentationFramework中的System.Windows.Controls.Button为例) 1.  声明依赖属性 public static read ...

  5. linux下文件和目录的颜色表示

    蓝色表示目录: 绿色表示可执行文件: 红色表示压缩文件: 浅蓝色表示链接文件: 灰色表示其它文件: 红色闪烁表示链接的文件有问题了: 黄色是设备文件,包括block, char, fifo. (摘自: ...

  6. Android线程之异步消息处理机制(一)

    Android不允许在子线程中进行UI操作,但是有些时候,我们必须在子线程里去执行一些耗时任务,然后根据任务的执行结果来更新相应的UI控件.对于这种情况,Android提供了一套异步消息处理机制,完美 ...

  7. [TJOI2013]单词

    2755: [TJOI2013]单词 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 6  Solved: 3[Submit][Status][Web B ...

  8. js实现的文章输入检查与测速。(纯js版本)

    朋友又提出一些需求.希望不要jquery .于是修改成js版本. <!DOCTYPE html> <html> <head> <meta charset=&q ...

  9. IOS9新特性之Contacts联系人

    在以前iOS开发中,涉及联系人相关的编程,代码都非常繁琐,并且框架的设计也不是Objective-C风格的,这使开发者用起来非常的难受.在iOS9中,apple终于解决了这个问题,全新的Contact ...

  10. IOS开发中长按的手势事件编程

    长按手势事件: 长按按钮1S后改变按钮颜色: // 长按事件 #import "ViewController.h" @interface ViewController (){ UI ...