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 mustache template engine #65

Merged
merged 1 commit into from
Jul 29, 2018
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Currently supports the following templates engines:
- [`pug`](https://pugjs.org/api/getting-started.html)
- [`handlebars`](http://handlebarsjs.com/)
- [`marko`](http://markojs.com/)
- [`mustache`](https://mustache.github.io/)

In `production` mode, `point-of-view` will heavily cache the templates file and functions, while in `development` will reload every time the template file and function.

Expand Down
21 changes: 20 additions & 1 deletion 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', 'nunjucks', 'pug', 'handlebars', 'marko', 'ejs-mate']
const supportedEngines = ['ejs', 'nunjucks', 'pug', 'handlebars', 'marko', 'ejs-mate', 'mustache']

function fastifyView (fastify, opts, next) {
if (!opts.engine) {
Expand All @@ -30,6 +30,7 @@ function fastifyView (fastify, opts, next) {
marko: viewMarko,
'ejs-mate': viewEjsMate,
handlebars: viewHandlebars,
mustache: viewMustache,
nunjucks: viewNunjucks,
_default: view
}
Expand Down Expand Up @@ -173,6 +174,24 @@ function fastifyView (fastify, opts, next) {
readFile(join(templatesDir, page), 'utf8', readCallback(this, page, data))
}

function viewMustache (page, data, opts) {
if (!page || !data) {
this.send(new Error('Missing data'))
return
}

const options = Object.assign({}, opts)

// append view extension
page = getPage(page, 'mustache')

readFile(join(templatesDir, page), 'utf8', (err, template) => {
if (err) return this.send(err)
let html = engine.render(template, data, options.partials)
this.header('Content-Type', 'text/html; charset=' + charset).send(html)
})
}

next()
}

Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"marko",
"nunjucks",
"pug",
"handlebars"
"handlebars",
"mustache"
],
"author": "Tomas Della Vedova - @delvedor (http://delved.org)",
"license": "MIT",
Expand All @@ -42,6 +43,7 @@
"fastify": "^1.3.1",
"handlebars": "^4.0.11",
"marko": "^4.12.1",
"mustache": "^2.3.0",
"nunjucks": "^3.1.2",
"pre-commit": "^1.2.2",
"pug": "^2.0.3",
Expand Down
33 changes: 33 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,39 @@ test('reply.view with handlebars engine', t => {
})
})

test('reply.view with mustache engine', t => {
t.plan(6)
const fastify = Fastify()
const mustache = require('mustache')
const data = { text: 'text' }

fastify.register(require('./index'), {
engine: {
mustache: mustache
}
})

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

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

sget({
method: 'GET',
url: '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; charset=utf-8')
t.strictEqual(mustache.render(fs.readFileSync('./templates/index.html', 'utf8'), data), body.toString())
fastify.close()
})
})
})

test('reply.view with marko engine', t => {
t.plan(6)
const fastify = Fastify()
Expand Down