mat-freemarker
Advanced tools
Comparing version 0.0.1 to 0.0.3
22
index.js
@@ -7,16 +7,9 @@ var fs = require('fs') | ||
var __CONFIG = { | ||
viewRoot: './', | ||
dataRoot: './' | ||
} | ||
var fetchData = function(filePath) { | ||
if (!filePath) return | ||
filePath = resolve(__CONFIG.dataRoot, './' + filePath) | ||
if (fs.existsSync(filePath)) { | ||
return fs.readFileSync(filePath).toString() | ||
} else { | ||
throw new Error('未找到对应的 ' + filePath + ' 文件,请检查数据') | ||
throw new Error('未找到对应的 ' + filePath + ' 文件,请检查数据是否正确') | ||
} | ||
@@ -44,9 +37,8 @@ } | ||
__CONFIG = Object.assign(__CONFIG, opts) | ||
return function* freemarker(next) { | ||
yield next | ||
var reg = new RegExp(path.normalize(opts.viewRoot)) | ||
var data = fetchData(this.path.replace(reg, '').split('.')[0] + '.js') | ||
var reg = new RegExp('/' + path.normalize(opts.viewRoot) + '/') | ||
var dataPath = this.path.replace(reg, path.normalize(opts.dataRoot) + '/').split('.')[0] + '.json' | ||
var data = fetchData(resolve(dataPath)) | ||
@@ -56,3 +48,3 @@ try { | ||
} catch(err) { | ||
throw new Error('数据:' + this.path.split('.')[0] + '.js' + ' 不是标准的JSON格式') | ||
throw new Error('数据:' + resolve(dataPath) + ' 不是标准的JSON格式') | ||
} | ||
@@ -62,3 +54,3 @@ | ||
data: data, | ||
tpl: path.basename(this.path) | ||
tpl: this.path.replace(reg, './') | ||
}) | ||
@@ -65,0 +57,0 @@ |
{ | ||
"name": "mat-freemarker", | ||
"version": "0.0.1", | ||
"version": "0.0.3", | ||
"description": "freemarker plugin for mat", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -18,6 +18,6 @@ # mat-freemarker | ||
.use(freemarker({ | ||
viewRoot: '/template', | ||
dataRoot: '/fakeData', | ||
viewRoot: './template', | ||
dataRoot: './fakeData', | ||
options: { | ||
numberFormat: '0.############' | ||
} | ||
@@ -30,3 +30,3 @@ })) | ||
* viewRoot: ftl模板目录 | ||
* dataRoot: 需要渲染的数据目录,文件命名与ftl模板保持一致,保存为 `.js` 文件 | ||
* dataRoot: 需要渲染的数据目录,文件名及路径与ftl模板保持一致,保存为 `.json` 文件 | ||
* See the freemarkerjs [options](https://github.com/ijse/freemarker.js#configurations) |
3705
44