tpl.js
846 Bytes
/**
* guang
* @author: lcy<chuanyang.liu@yoho.cn>
* @date: 2016/09/01
*/
'use strict';
let exphbs = require('express-handlebars').create();
let Handlebars = exphbs.handlebars;
let fs = require('fs');
Handlebars.registerHelper('tpl', function(id, file, options) {
let tpl = '';
if (options.fn) {
tpl = options.fn(this);
} else {
let source = Handlebars.partials[file];
if (source === null) {
source = fs.readFileSync('views/' + file + '.html', 'utf8');
if (!source) {
new Error('file is not found : ' + file);
}
}
tpl += '<script id="' + id + '" type="text/x-handlebars" data-file="' + file + '">';
tpl += source;
tpl += '</script>';
}
return new Handlebars.SafeString(tpl);
});
module.exports = Handlebars;