Skip to content

Commit

Permalink
bug fix #76
Browse files Browse the repository at this point in the history
  • Loading branch information
唐斌 committed Jun 18, 2014
1 parent 3b17ed7 commit 91b5e11
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 26 deletions.
10 changes: 9 additions & 1 deletion README.md
Expand Up @@ -178,7 +178,11 @@ encoding | String | ``'utf-8'`` | 指定模板编码

### NodeJS + Express

app.register('.html', require('art-template'));
var template = require('art-template');
template.config('extname', '.html');
app.engine('.html', template.__express);
app.set('view engine', 'html');
//app.set('views', __dirname + '/views');

> 若使用 js 原生语法作为模板语法,请改用 ``require('art-template/node/template-native.js')``
Expand All @@ -203,6 +207,10 @@ encoding | String | ``'utf-8'`` | 指定模板编码

## 更新日志

### v3.0.1

1. 适配 express3.x 与 4.x,修复路径 BUG

### v3.0.0

1. 提供 NodeJS 专属版本,支持使用路径加载模板,并且模板的``include``语句也支持相对路径
Expand Down
48 changes: 48 additions & 0 deletions demo/node-template-express.js
@@ -0,0 +1,48 @@
var express = require('express');
var template = require('../node/template.js');


var app = module.exports = express();

template.config('extname', '.html');
app.engine('.html', template.__express);
app.set('view engine', 'html');
app.set('views', __dirname + '/node-template');


var demoData = {
title: '国内要闻',
time: (new Date).toString(),
list: [
{
title: '<油价>调整周期缩至10个工作日 无4%幅度限制',
url: 'http://finance.qq.com/zt2013/2013yj/index.htm'
},
{
title: '明起汽油价格每吨下调310元 单价回归7元时代',
url: 'http://finance.qq.com/a/20130326/007060.htm'
},
{
title: '广东副县长疑因抛弃情妇遭6女子围殴 纪检调查',
url: 'http://news.qq.com/a/20130326/001254.htm'
},
{
title: '湖南27岁副县长回应质疑:父亲已不是领导',
url: 'http://news.qq.com/a/20130326/000959.htm'
},
{
title: '朝军进入战斗工作状态 称随时准备导弹攻击美国',
url: 'http://news.qq.com/a/20130326/001307.htm'
}
]
};

app.get('/', function(req, res){
res.render('index', demoData);
});

/* istanbul ignore next */
if (!module.parent) {
app.listen(3000);
console.log('Express started on port 3000');
}
32 changes: 21 additions & 11 deletions node/_node.js
Expand Up @@ -4,12 +4,13 @@ var path = require('path');
module.exports = function (template) {

var cacheStore = template.cache;

var defaults = template.defaults;
var rExtname;

// 提供新的配置字段
template.defaults.base = '';
template.defaults.extname = '.html';
template.defaults.encoding = 'utf-8';
defaults.base = '';
defaults.extname = '.html';
defaults.encoding = 'utf-8';


// 重写引擎编译结果获取方法
Expand All @@ -35,21 +36,21 @@ module.exports = function (template) {


function readTemplate (id) {
id = path.join(template.defaults.base, id + template.defaults.extname);
id = path.join(defaults.base, id + defaults.extname);

if (id.indexOf(template.defaults.base) !== 0) {
if (id.indexOf(defaults.base) !== 0) {
// 安全限制:禁止超出模板目录之外调用文件
throw new Error('"' + id + '" is not in the template directory');
} else {
try {
return fs.readFileSync(id, template.defaults.encoding);
return fs.readFileSync(id, defaults.encoding);
} catch (e) {}
}
}


// 重写模板`include``语句实现方法,转换模板为绝对路径
template.helpers.$include = function (filename, data, from) {
template.utils.$include = function (filename, data, from) {

from = path.dirname(from);
filename = path.join(from, filename);
Expand All @@ -59,15 +60,24 @@ module.exports = function (template) {


// express support
template.__express = function (path, options, fn) {
template.__express = function (file, options, fn) {

if (typeof options === 'function') {
fn = options;
options = {};
}

options.filename = path;
fn(null, template.renderFile(path, options));

if (!rExtname) {
// 去掉 express 传入的路径
rExtname = new RegExp((defaults.extname + '$').replace(/\./g, '\\.'));
}


file = file.replace(rExtname, '');

options.filename = file;
fn(null, template.renderFile(file, options));
};


Expand Down
32 changes: 18 additions & 14 deletions package.json
@@ -1,22 +1,26 @@
{
"name" : "art-template",
"description" : "JavaScript Template Engine",
"homepage" : "http://aui.github.com/artTemplate/",
"keywords" : ["util", "functional", "template"],
"author" : "tangbin <sugarpie.tang@gmail.com>",
"repository" : {"type": "git", "url": "git://github.com/aui/artTemplate.git"},
"main" : "./node/template.js",
"version" : "3.0.0",
"name": "art-template",
"description": "JavaScript Template Engine",
"homepage": "http://aui.github.com/artTemplate/",
"keywords": [
"util",
"functional",
"template"
],
"author": "tangbin <sugarpie.tang@gmail.com>",
"repository": {
"type": "git",
"url": "git://github.com/aui/artTemplate.git"
},
"main": "./node/template.js",
"version": "3.0.1",
"devDependencies": {
"grunt-cli": "*",
"express": "~4.4.3",
"grunt-cli": "*",
"grunt": "*",
"grunt-contrib-jshint": "*",
"grunt-contrib-concat": "*",
"grunt-contrib-uglify": "*"
},
"license" : "BSD"
"license": "BSD"
}




0 comments on commit 91b5e11

Please sign in to comment.