-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0d80864
commit 4a130f0
Showing
6 changed files
with
271 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"parser": "babel-eslint", | ||
"env": { | ||
"node": true, | ||
"mocha": true, | ||
"es6": true // es6 envoriment | ||
}, | ||
"rules": { | ||
"strict": [0], | ||
"eqeqeq": 2, | ||
"quotes": [2, "single", {"allowTemplateLiterals": true}], | ||
"no-underscore-dangle": 0, | ||
"eol-last": 0, | ||
"camelcase": 0, | ||
"no-loop-func": 0, | ||
"no-trailing-spaces": 0, | ||
"consistent-return": 0, | ||
"new-cap": 0, | ||
"no-shadow": 0, | ||
"semi": 0, | ||
"no-process-exit": 0, | ||
"no-empty": 0, | ||
"yoda": 0, | ||
"no-const-assign": "warn", // more rule | ||
"no-new-func": 0, | ||
"no-labels": 0, | ||
"no-this-before-super": "warn", // more rule | ||
"no-undef": "warn", // more rule | ||
"no-unreachable": "warn", // more rule | ||
"no-unused-vars": "warn", // more rule | ||
"valid-typeof": "warn" // more rule | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
language: node_js | ||
node_js: | ||
- '6' | ||
sudo: false | ||
script: | ||
- "npm test" | ||
after_success: | ||
- 'npm install coveralls && ./node_modules/.bin/nyc report --reporter=text-lcov | ./node_modules/.bin/coveralls' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,70 @@ | ||
# think-view-ejs | ||
compile view templates with EJS for ThinkJS 3.x | ||
[![Build Status](https://img.shields.io/travis/thinkjs/think-view-ejs/master.svg?style=flat-square)](https://travis-ci.org/thinkjs/think-view-ejs) | ||
[![Coverage Status](https://img.shields.io/coveralls/thinkjs/think-view-ejs/master.svg?style=flat-square)](https://coveralls.io/github/thinkjs/think-view-ejs?branch=master) | ||
[![npm](https://img.shields.io/npm/v/think-view-ejs.svg?colorB=brightgreen&style=flat-square)](https://www.npmjs.com/package/think-view-ejs) | ||
|
||
|
||
Compile view templates with EJS for ThinkJS 3.x. | ||
|
||
|
||
## Install | ||
|
||
``` | ||
npm install think-view-ejs | ||
``` | ||
## Usage | ||
|
||
edit config file `src/config/adapter.js`, add options for ejs adapter: | ||
|
||
``` | ||
const ejs = require('think-view-ejs'); | ||
exports.view = { | ||
type: 'ejs', | ||
ejs: { | ||
//options | ||
handle: ejs, | ||
beforeRender: (ejs, config) => { | ||
//do something before render the template. | ||
} | ||
} | ||
} | ||
``` | ||
|
||
|
||
|
||
### Options | ||
|
||
Default options: | ||
|
||
``` | ||
const defaultOptions = { | ||
cache: true | ||
} | ||
``` | ||
|
||
You can override it and add other options by editing file `src/config/adapter.js` : | ||
|
||
```` | ||
exports.view = { | ||
type: 'ejs', | ||
ejs: { | ||
//override `cache` option | ||
cache: false, | ||
root: '/public/path', | ||
handle: ejs, | ||
beforeRender: (ejs, config) => { | ||
//do something before render the template. | ||
} | ||
} | ||
} | ||
```` | ||
|
||
Please refer to [https://github.com/mde/ejs#options](https://github.com/mde/ejs#options) for more information on EJS options. | ||
|
||
### beforeRender | ||
|
||
`beforeRender` is a function that you can handle something before rendering the template file. It exposes 2 parameters: | ||
|
||
* `ejs` — the original `ejs` module | ||
* `config` — current configure of ejs adapter | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
const ejs = require('ejs'); | ||
const helper = require('think-helper'); | ||
/** | ||
* ejs default render options | ||
* for more information on ejs options, refer to https://github.com/mde/ejs#options | ||
*/ | ||
const defaultOptions = { | ||
cache: true | ||
}; | ||
|
||
class Ejs { | ||
/** | ||
* @param {String} file: filename(absolute path)of template | ||
* @param {Object} data | ||
* @param {Object} config | ||
*/ | ||
constructor(file, data, config) { | ||
this.file = file; | ||
this.data = data; | ||
this.config = helper.extend({ | ||
filename: file | ||
}, defaultOptions, config); | ||
} | ||
/** | ||
* render | ||
* @return {Promise} | ||
*/ | ||
render() { | ||
const config = this.config; | ||
if(config.beforeRender) { | ||
config.beforeRender(ejs, config); | ||
} | ||
let fn = helper.promisify(ejs.renderFile); | ||
return fn(this.file, this.data, this.config); | ||
} | ||
} | ||
|
||
module.exports = Ejs; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ | ||
"name": "think-view-ejs", | ||
"version": "0.0.1", | ||
"description": "compile view templates with EJS for ThinkJS 3.x", | ||
"author": { | ||
"name": "huangxiaolu", | ||
"email": "lu.huang90@gmail.com" | ||
}, | ||
"scripts": { | ||
"test": "eslint index.js && nyc ava test/", | ||
"coverage": "nyc report --reporter=html" | ||
}, | ||
"dependencies": { | ||
"think-helper": "^1.0.0", | ||
"ejs": "^2.5.6" | ||
}, | ||
"devDependencies": { | ||
"ava": "^0.18.0", | ||
"babel-core": "^6.22.1", | ||
"babel-eslint": "^7.1.1", | ||
"eslint": "2.8.0", | ||
"nyc": "^7.0.0", | ||
"mock-require": "^2.0.1" | ||
}, | ||
"contributors": [ | ||
{ | ||
"name": "huangxiaolu", | ||
"email": "lu.huang90@gmail.com" | ||
}, | ||
{ | ||
"name": "welefen", | ||
"email": "welefen@gmail.com" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import test from 'ava'; | ||
import thinkViewEjs from '../index.js'; | ||
import ejs from 'ejs'; | ||
|
||
|
||
test.beforeEach(t => { | ||
ejs.___renderFile = ejs.renderFile; | ||
}); | ||
|
||
|
||
function callEjsView(file, viewData, config) { | ||
const instance = new thinkViewEjs(file, viewData, config); | ||
return Promise.resolve(instance.render()); | ||
} | ||
test('ejs render with beforeRender', (t) => { | ||
const filename = '/file/path/../x.ejs'; | ||
const tpl = '<%= name %>'; | ||
const result = 'huangxiaolu'; | ||
const viewData = { | ||
name: 'huangxiaolu' | ||
}; | ||
const config = { | ||
beforeRender: function(ejs, conf) { | ||
|
||
} | ||
}; | ||
function mockRenderFile() { | ||
ejs.renderFile = function(filename, data, config, cb) { | ||
const err = null; | ||
const str = result; | ||
cb(err, str); | ||
} | ||
} | ||
|
||
mockRenderFile(); | ||
callEjsView(filename, viewData, config).then((str) => { | ||
t.is(str, result) | ||
}); | ||
}); | ||
test('ejs render without beforeRender', (t) => { | ||
const filename = '/file/path/../x.ejs'; | ||
const tpl = '<%= name %>'; | ||
const result = 'huangxiaolu'; | ||
const viewData = { | ||
name: 'huangxiaolu' | ||
}; | ||
const config = {}; | ||
function mockRenderFile() { | ||
ejs.renderFile = function(filename, data, config, cb) { | ||
const err = null; | ||
const str = result; | ||
cb(err, str); | ||
} | ||
} | ||
|
||
mockRenderFile(); | ||
callEjsView(filename, viewData, config).then((str) => { | ||
t.is(str, result) | ||
}); | ||
}); | ||
test('ejs render error', (t) => { | ||
const filename = '/file/path/../x.ejs'; | ||
const tpl = '<%= name %>'; | ||
const result = 'huangxiaolu'; | ||
const viewData = { | ||
name: 'huangxiaolu' | ||
}; | ||
const errmsg = "file not exist"; | ||
const config = {}; | ||
function mockRenderFile() { | ||
ejs.renderFile = function(filename, data, config, cb) { | ||
const err = errmsg; | ||
const str = result; | ||
cb(err, str); | ||
} | ||
} | ||
|
||
mockRenderFile(); | ||
callEjsView(filename, viewData, config).then(str => { | ||
|
||
}).catch(err => { | ||
t.is(err, errmsg); | ||
}); | ||
}); | ||
test.afterEach.always(t => { | ||
ejs.renderFile = ejs.___renderFile; | ||
}); |