Skip to content

Commit

Permalink
dot: layout option (#210)
Browse files Browse the repository at this point in the history
  • Loading branch information
alex2844 committed Feb 10, 2021
1 parent 56342e0 commit e4c9bb2
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 5 deletions.
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ function fastifyView (fastify, opts, next) {
const defaultCtx = opts.defaultContext || {}
const layoutFileName = opts.layout

if (layoutFileName && type !== 'handlebars' && type !== 'ejs' && type !== 'eta') {
next(new Error('Only Handlebars, EJS, and Eta support the "layout" option'))
if (layoutFileName && type !== 'dot' && type !== 'handlebars' && type !== 'ejs' && type !== 'eta') {
next(new Error('Only Dot, Handlebars, EJS, and Eta support the "layout" option'))
return
}

if (layoutFileName && !hasAccessToLayoutFile(layoutFileName)) {
if (layoutFileName && !hasAccessToLayoutFile(layoutFileName + ((type === 'dot') ? '.dot' : ''))) {
next(new Error(`unable to access template "${layoutFileName}"`))
return
}
Expand All @@ -57,7 +57,7 @@ function fastifyView (fastify, opts, next) {
'art-template': viewArtTemplate,
twig: viewTwig,
liquid: viewLiquid,
dot: dotRender,
dot: withLayout(dotRender),
eta: withLayout(viewEta),
_default: view
}
Expand Down
3 changes: 3 additions & 0 deletions templates/layout.dot
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
header: {{! it.text }}
{{= it.body }}
footer
35 changes: 35 additions & 0 deletions test/test-dot.js
Original file line number Diff line number Diff line change
Expand Up @@ -447,3 +447,38 @@ test('fastify.view with dot engine, should throw page missing', t => {
})
})
})

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

fastify.register(require('../index'), {
engine: {
dot: engine
},
root: 'templates',
layout: 'layout'
})

fastify.get('/', (req, reply) => {
reply.view('testdot', 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('header: textfoo text1 <p>foo</p>footer', body.toString())
fastify.close()
})
})
})
2 changes: 1 addition & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ test('register callback should throw if layout option provided with wrong engine
layout: 'template'
}).ready(err => {
t.ok(err instanceof Error)
t.is(err.message, 'Only Handlebars, EJS, and Eta support the "layout" option')
t.is(err.message, 'Only Dot, Handlebars, EJS, and Eta support the "layout" option')
})
})

Expand Down

0 comments on commit e4c9bb2

Please sign in to comment.