Skip to content

Commit

Permalink
viewFile is already absolute path
Browse files Browse the repository at this point in the history
  • Loading branch information
lushijie committed Mar 27, 2017
1 parent f6f0543 commit 0e8f11d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
18 changes: 6 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
* @Author: lushijie
* @Date: 2017-03-10 09:38:38
* @Last Modified by: lushijie
* @Last Modified time: 2017-03-27 12:23:30
* @Last Modified time: 2017-03-27 17:08:45
*/
const helper = require('think-helper');
const path = require('path');
const handlebars = require('handlebars');
const fs = require('fs');
const readFile = helper.promisify(fs.readFile, fs);
Expand Down Expand Up @@ -43,25 +42,20 @@ class Handlebars {
* render view file
*/
render() {
let viewPath = this.config.viewPath;
let viewFile = this.viewFile;

if(this.config.beforeRender){
this.config.beforeRender(handlebars, this.config);
}

let absolutePath = this.viewFile;
if(!path.isAbsolute(absolutePath)){
absolutePath = path.join(viewPath, absolutePath);
if(this.config.cache && cacheFn[viewFile]) {
return Promise.resolve(cacheFn[viewFile](this.viewData));
}

if(this.config.cache && cacheFn[absolutePath]) {
return Promise.resolve(cacheFn[absolutePath](this.viewData));
}

return readFile(absolutePath, 'utf8').then((data) => {
return readFile(viewFile, 'utf8').then((data) => {
let compileFn = handlebars.compile(data, this.config);
if(this.config.cache) {
cacheFn[absolutePath] = compileFn;
cacheFn[viewFile] = compileFn;
}
return compileFn(this.viewData);
});
Expand Down
8 changes: 5 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: lushijie
* @Date: 2017-02-14 10:56:08
* @Last Modified by: lushijie
* @Last Modified time: 2017-03-13 15:06:11
* @Last Modified time: 2017-03-27 17:05:37
*/
import test from 'ava';
import helper from 'think-helper';
Expand All @@ -23,8 +23,9 @@ let viewData = {title: 'Thinkjs'};

// test case
test.serial('handlebars default render', async t => {
let viewFile = path.join(viewBasePath, './home.tpl');
let config = helper.extend({}, defaultOptions, {viewPath: viewBasePath});
let handlebars = new Handlebars('./home.tpl', viewData, config);
let handlebars = new Handlebars(viewFile, viewData, config);
let fileContent = fs.readFileSync(path.join(viewBasePath, 'home.tpl')).toString();
let originResp = handlebarsOrigin.compile(fileContent)(viewData);

Expand Down Expand Up @@ -68,7 +69,8 @@ test.serial('handlebars with registerHelper', async t => {
handlebars.registerHelper('shorten', shortenFn);
}
});
let handlebars = new Handlebars('./admin.tpl', viewData, config);
let viewFile = path.join(viewBasePath, './admin.tpl');
let handlebars = new Handlebars(viewFile, viewData, config);
handlebarsOrigin.registerHelper('shorten', shortenFn);
let fileContent = fs.readFileSync(path.join(__dirname, 'views/admin.tpl')).toString();
let originResp = handlebarsOrigin.compile(fileContent)(viewData);
Expand Down

0 comments on commit 0e8f11d

Please sign in to comment.