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

dot: layout option #210

Merged
merged 1 commit into from
Feb 10, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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