在Express中安装XTemplate
来源:互联网 发布:unity3d自学教程 编辑:程序博客网 时间:2024/05/17 19:19
上一节讲了安装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, callback
is 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 moduleapp.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 directoryapp.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
- 在win7系统中安装 Outlook Express
- Ext中使用XTemplate
- ExtJs中XTemplate实例
- 在sql server express中安装示例数据库
- 在express中使用websocket
- DB2-Express C 在Linux AS 5中安装中libstdc++.so.5的问题
- 在Red Hat Linux AS 5 中安装 Oracle Database Express Edition 10.2
- 在windows7中无法安装 SQL Server 2008 Management Studio Express
- 在win7 + vs express for desktop中安装stlport和boost库
- 在FedaroCore4下安装DB2 Express-C
- 在Ubuntu 上安装 DB2 Express-C
- nodejs 在Windows安装步骤框架express
- 在windown下安装DB2-Express-C
- Ext Js中Ext.XTemplate使用方法学习
- ExtJs中Ext.XTemplate使用方法学习
- 如何在Outlook Express中使用MathType
- 在Express中使用模板引擎
- JAVA模拟银行存钱,取钱操作以及计算利息操作
- Android 最火的快速开发框架AndroidAnnotations使用详解
- Ucgui列表
- 定时任务中,cronExpression的设置规则
- matlab读取img格式文件
- 在Express中安装XTemplate
- XMPP会议室设置MUC
- LAMP几个常用配置
- Oracle Applications DBA 基础(二)
- Android 开源框架Universal-Image-Loader完全解析(一)--- 基本介绍及使用 .
- Java中的正则表达式
- centos强制踢掉某登录用户
- Android开发小笔记
- 【Android】fragment生命周期