smarty/index.js

119 lines
2.4 KiB
JavaScript
Raw Normal View History

2018-05-25 15:35:26 +08:00
/**
2020-04-19 11:27:07 +08:00
* nodeJS 模板引擎
2020-09-18 14:16:26 +08:00
* @author yutent<yutent.io@gmail.com>
* @date 2020/09/18 13:36:47
2018-05-25 15:35:26 +08:00
*/
require('es.shim')
2020-04-07 00:49:07 +08:00
2018-05-25 15:35:26 +08:00
const Tool = require('./lib/tool')
2020-04-06 23:56:03 +08:00
const path = require('path')
2020-04-19 11:27:07 +08:00
const fs = require('iofs')
2020-04-06 23:33:47 +08:00
function hash(str) {
return Buffer.from(str).toString('hex')
}
2018-05-25 15:35:26 +08:00
2020-09-27 19:32:13 +08:00
var cacheDir = path.resolve(__dirname, './cache/')
2018-05-25 15:35:26 +08:00
class Smarty {
constructor(opt) {
2020-09-27 19:32:13 +08:00
this.opt = { ext: '.htm' }
2018-05-25 15:35:26 +08:00
if (opt) {
Object.assign(this.opt, opt)
}
2020-04-06 23:33:47 +08:00
this.__REG__ = new RegExp(this.opt.ext + '$')
2020-04-07 00:49:07 +08:00
this.tool = new Tool(this.opt)
2020-09-27 19:32:13 +08:00
this.reset()
2020-04-07 01:01:41 +08:00
this.__CACHE__ = Object.create(null) // 渲染缓存
2020-09-27 19:32:13 +08:00
// 消除缓存目录
fs.rm(cacheDir)
}
reset() {
this.__DATA__ = Object.create(null) // 预定义的变量储存
2018-05-25 15:35:26 +08:00
}
config(key, val) {
2020-04-06 23:56:03 +08:00
key += ''
2020-04-07 00:49:07 +08:00
if (!key || val === undefined) {
2020-04-06 23:56:03 +08:00
return
}
this.opt[key] = val
2020-04-07 00:49:07 +08:00
this.tool.opt[key] = val
2018-05-25 15:35:26 +08:00
}
/**
* 定义变量
* @param {Str} key 变量名
* @param {any} val
*/
assign(key, val) {
key += ''
if (!key) {
return this
}
2020-04-06 23:33:47 +08:00
this.__DATA__[key] = val
2018-05-25 15:35:26 +08:00
return this
}
/**
* [render 模板渲染]
2020-09-27 19:32:13 +08:00
* @param {String} filePath 模板路径
2020-04-06 23:33:47 +08:00
* @param {Boolean} noParse 不解析直接读取
2018-05-25 15:35:26 +08:00
* @return {Promise} 返回一个Promise对象
*/
2020-09-27 19:32:13 +08:00
render(filePath = '', noParse = false) {
var key, ckey, cache, needWrite
2020-04-07 00:49:07 +08:00
if (!this.opt.path) {
2018-05-25 15:35:26 +08:00
throw new Error('Smarty engine must define path option')
}
2020-09-27 19:32:13 +08:00
if (!filePath) {
return Promise.reject('argument[filePath] can not be empty')
2018-05-25 15:35:26 +08:00
}
2020-09-27 19:32:13 +08:00
if (!this.__REG__.test(filePath)) {
filePath += this.opt.ext
2018-05-25 15:35:26 +08:00
}
2020-09-27 19:32:13 +08:00
filePath = path.resolve(this.opt.path, filePath)
2018-05-25 15:35:26 +08:00
2020-09-27 19:32:13 +08:00
key = hash(filePath)
ckey = path.join(cacheDir, key)
2018-05-25 15:35:26 +08:00
2020-04-07 00:49:07 +08:00
if (this.__CACHE__[key]) {
2020-09-27 19:32:13 +08:00
cache = fs.cat(ckey)
} else {
cache = fs.cat(filePath)
this.__CACHE__[key] = true
needWrite = true
2018-05-25 15:35:26 +08:00
}
2020-09-27 19:32:13 +08:00
// 无需解析时, 直接输出
2020-04-06 23:33:47 +08:00
if (noParse) {
2020-09-27 19:32:13 +08:00
if (needWrite) {
fs.echo(cache, ckey)
}
2020-04-07 01:01:41 +08:00
return Promise.resolve(cache)
2020-09-27 19:32:13 +08:00
} else {
if (needWrite) {
cache = this.tool.parse(cache)
fs.echo(cache, ckey)
}
2020-04-06 23:33:47 +08:00
}
2018-05-25 15:35:26 +08:00
try {
2020-09-27 19:32:13 +08:00
var body = this.tool.exec(cache, this.__DATA__)
this.reset()
return Promise.resolve(body)
2018-05-25 15:35:26 +08:00
} catch (err) {
return Promise.reject(err)
}
}
}
module.exports = Smarty