tpl.js 755 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){

    if(options.fn) {

    }
    var source = Handlebars.partials[file];
    var tpl = '';
    if(source == null) {
        var 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;