Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for ejs-mate #16

Merged
merged 6 commits into from
Sep 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@ jspm_packages

# marko generated files
*.marko.js
yarn.lock
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Templates rendering plugin support for Fastify.

Currently supports the following templates engines:
- [`ejs`](http://www.embeddedjs.com/)
- [`ejs-mate`](https://github.com/JacksonTian/ejs-mate)
- [`pug`](https://pugjs.org/api/getting-started.html)
- [`handlebars`](http://handlebarsjs.com/)
- [`marko`](http://markojs.com/)
Expand Down
29 changes: 26 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const readFile = require('fs').readFile
const resolve = require('path').resolve
const join = require('path').join
const HLRU = require('hashlru')
const supportedEngines = ['ejs', 'pug', 'handlebars', 'marko']
const supportedEngines = ['ejs', 'pug', 'handlebars', 'marko', 'ejs-mate']

function fastifyView (fastify, opts, next) {
if (!opts.engine) {
Expand All @@ -24,8 +24,13 @@ function fastifyView (fastify, opts, next) {
const templatesDir = resolve(opts.templates || './')
const lru = HLRU(opts.maxCache || 100)
const prod = process.env.NODE_ENV === 'production'
const renders = {
marko: viewMarko,
'ejs-mate': viewEjsMate,
_default: view
}

fastify.decorateReply('view', type === 'marko' ? viewMarko : view)
fastify.decorateReply('view', renders[type] ? renders[type] : renders._default)

function view (page, data) {
if (!page || !data) {
Expand Down Expand Up @@ -58,11 +63,29 @@ function fastifyView (fastify, opts, next) {
if (!that.res.getHeader('content-type')) {
that.header('Content-Type', 'text/html')
}

that.send(lru.get(page)(data))
}
}

function viewEjsMate (page, data) {
if (!page || !data) {
this.send(new Error('Missing data'))
return
}
const confs = Object.assign({}, options)
if (!confs.settings) {
confs.settings = {}
}
// ejs-mate use views to find layouts
confs.settings.views = templatesDir
// setting locals to pass data by
confs.locals = Object.assign({}, confs.locals, data)
engine(join(templatesDir, page), confs, (err, html) => {
if (err) return this.send(err)
this.header('Content-Type', 'text/html').send(html)
})
}

function viewMarko (page, data, opts) {
if (!page || !data) {
this.send(new Error('Missing data'))
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
},
"devDependencies": {
"ejs": "^2.5.6",
"ejs-mate": "^2.3.0",
"express": "^4.15.2",
"fastify": "^0.24.0",
"handlebars": "^4.0.6",
Expand Down
1 change: 1 addition & 0 deletions templates/content.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<% layout('./templates/layout') -%><div><%= text %></div>
1 change: 1 addition & 0 deletions templates/layout.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<html><head></head><body><h1><%= header %></h1><%- body -%><div><%= footer %></div></body></html>
33 changes: 33 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,3 +355,36 @@ test('reply.view with pug engine, will preserve content-type', t => {
})
})
})

test('reply.view with ejs-mate engine', t => {
t.plan(6)
const fastify = Fastify()
const ejsMate = require('ejs-mate')
const data = { text: 'text', header: 'header', footer: 'footer' }

fastify.register(require('./index'), {
engine: {
'ejs-mate': ejsMate
}
})

fastify.get('/', (req, reply) => {
reply.view('/templates/content.ejs', data)
})

fastify.listen(0, err => {
t.error(err)

request({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 200)
t.strictEqual(response.headers['content-length'], '' + body.length)
t.strictEqual(response.headers['content-type'], 'text/html')
t.strictEqual('<html><head></head><body><h1>header</h1><div>text</div><div>footer</div></body></html>', body)
fastify.close()
})
})
})