Authored by 刘传洋

code refresh

/**
* guang
* @author: lcy<chuanyang.liu@yoho.cn>
* @date: 2016/09/01
*/
'use strict';
let fs = require('fs');
// exphbs = require('express-handlebars').create(),
// app = express(),
function wrap(app, exphbs) {
let vPath = app.get('views') || 'views/',
extName = app.get('view engine') || '.hbs',
Handlebars = exphbs.handlebars;
Handlebars.registerHelper('include', function(partial, options) {
console.log('-------');
// console.log(exphbs);
// console.log(exphbs);
// console.log('==================');
// console.log(Handlebars.partials);
let tmpl = Handlebars.partials[partial] || exphbs.partials[partial];
// let encoding = options.hash['encoding'] || 'utf8';
exphbs.getPartials().then(function(partials) {
console.log(partials.length);
}).catch(function() {
});
if (tmpl === null) {
let source = fs.readFileSync(`${vPath}/${partial}${extName}`, 'utf8');
if (source !== null) {
Handlebars.registerPartial(partial, source);
tmpl = Handlebars.partials[partial];
} else {
new Error('partial is not found : ' + partial);
}
}
// options.fn(this);
let ctx = Object.create(this);
for (let i in options.hash) {
if (options.hash.hasOwnProperty(options.hash[i])) {
ctx[i] = options.hash[i];
}
}
if (typeof tmpl !== 'function') {
tmpl = Handlebars.compile(tmpl);
}
return new Handlebars.SafeString(tmpl(ctx, options));
});
}
module.exports = wrap;
/**
* 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;
... ... @@ -9,8 +9,6 @@
path = require('path'),
hbs = require('express-handlebars');
// glob = require('glob');
var app = express();
// set view engin
... ... @@ -23,36 +21,20 @@
Object.assign(app.locals, parent.locals);
});
// let mhbs = hbs.create({
// extname: '.hbs',
// defaultLayout: 'layout',
// layoutsDir: doraemon,
// partialsDir: [`${partials}/partial`, `${doraemon}/partial`],
// helpers: global.yoho.helpers
// });
app.set('views', path.join(__dirname, 'views/action'));
app.engine('.hbs', hbs({
extname: '.hbs',
defaultLayout: 'layout',
layoutsDir: doraemon,
partialsDir: [`${partials}/partial`, `${doraemon}/partial`],
helpers: global.yoho.helpers
}));
// register helper
// var helpersDir = path.join(__dirname, 'helpers');
// glob.sync(helpersDir + '/**/*.+(js|coffee)').forEach(function(file) {
// console.log(file);
// require(file);
// })
// require('./helpers/include')(app, mhbs);
require('./helpers/json');
require('./helpers/page');
require('./helpers/tpl');
app.set('views', path.join(__dirname, 'views/action'));
app.engine('.hbs', hbs({
extname: '.hbs',
defaultLayout: 'layout',
layoutsDir: doraemon,
partialsDir: [`${partials}/partial`, `${doraemon}/partial`],
helpers: global.yoho.helpers
}));
require('./helpers/json');
require('./helpers/page');
// router
app.use(require('./router'));
app.use(require('./router'));
module.exports = app;
module.exports = app;
... ...