Skip to content

Commit ed1511c

Browse files
authored
fix: support mustache partials as documented (#471)
1 parent 13647e2 commit ed1511c

File tree

3 files changed

+88
-1
lines changed

3 files changed

+88
-1
lines changed

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,9 +458,10 @@ async function fastifyView (fastify, opts) {
458458
// append view extension
459459
page = getPage(page, 'mustache')
460460
}
461+
const partials = Object.assign({}, globalOptions.partials || {}, options.partials || {})
461462
const requestedPath = getRequestedPath(this)
462463
const templateString = await getTemplate(page)
463-
const partialsObject = await getPartials(page, { partials: options.partials || {}, requestedPath })
464+
const partialsObject = await getPartials(page, { partials, requestedPath })
464465

465466
let html
466467
if (typeof templateString === 'function') {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head></head>
4+
<body>
5+
{{> partial1 }}
6+
{{> partial2 }}
7+
</body>
8+
</html>

test/test-mustache.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,84 @@ test('reply.view for mustache engine with data-parameter and reply.locals and de
273273
await fastify.close()
274274
})
275275

276+
test('reply.view with mustache engine with global partials', async t => {
277+
t.plan(4)
278+
const fastify = Fastify()
279+
const mustache = require('mustache')
280+
const data = { text: 'text' }
281+
282+
fastify.register(require('../index'), {
283+
engine: {
284+
mustache
285+
},
286+
options: {
287+
partials: {
288+
body: './templates/body.mustache'
289+
}
290+
}
291+
})
292+
293+
fastify.get('/', (_req, reply) => {
294+
reply.view('./templates/index.mustache', data)
295+
})
296+
297+
await fastify.listen({ port: 0 })
298+
299+
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
300+
const responseContent = await result.text()
301+
302+
t.assert.strictEqual(result.status, 200)
303+
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
304+
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
305+
t.assert.strictEqual(mustache.render(fs.readFileSync('./templates/index.mustache', 'utf8'), data, { body: '<p>{{ text }}</p>' }), responseContent)
306+
307+
await fastify.close()
308+
})
309+
310+
test('reply.view with mustache engine with global and local partials', async t => {
311+
t.plan(4)
312+
const fastify = Fastify()
313+
const mustache = require('mustache')
314+
const data = {}
315+
316+
fastify.register(require('../index'), {
317+
engine: {
318+
mustache
319+
},
320+
options: {
321+
partials: {
322+
partial1: './templates/partial-1.mustache'
323+
}
324+
}
325+
})
326+
327+
fastify.get('/', (_req, reply) => {
328+
reply.view('./templates/index-with-2-partials.mustache', data, { partials: { partial2: './templates/partial-2.mustache' } })
329+
})
330+
331+
await fastify.listen({ port: 0 })
332+
333+
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
334+
const responseContent = await result.text()
335+
336+
t.assert.strictEqual(result.status, 200)
337+
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
338+
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
339+
t.assert.strictEqual(
340+
mustache.render(
341+
fs.readFileSync('./templates/index-with-2-partials.mustache', 'utf8'),
342+
data,
343+
{
344+
partial1: 'Partial 1 - b4d932b9-4baa-4c99-8d14-d45411b9361e\n', // defined globally
345+
partial2: 'Partial 2 - fdab0fe2-6dab-4429-ae9f-dfcb791d1d3d\n' // defined locally
346+
}
347+
),
348+
responseContent
349+
)
350+
351+
await fastify.close()
352+
})
353+
276354
test('reply.view with mustache engine with partials', async t => {
277355
t.plan(4)
278356
const fastify = Fastify()

0 commit comments

Comments
 (0)