From 3abf8407697d556197bda06613dee854565d422f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amer=20Alimanovi=C4=87?= <22625371+amer8@users.noreply.github.com> Date: Mon, 27 May 2019 14:25:10 +0200 Subject: [PATCH] Partials support for Handlebars engine (#121) * Partials support for Handlebars engine * Fix test object.entries to object.keys * Separate tests into multiple files (#122) * Separate tests into multiple files * Fix npm scripts and add snapshot * Pass J flag to tap * Partials support for Handlebars engine * Fix lint --- README.md | 10 +++++++ index.js | 46 ++++++++++++++++++++++--------- templates/body.hbs | 1 + templates/index-with-partials.hbs | 7 +++++ test/test-handlebars.js | 35 +++++++++++++++++++++++ 5 files changed, 86 insertions(+), 13 deletions(-) create mode 100644 templates/body.hbs create mode 100644 templates/index-with-partials.hbs diff --git a/README.md b/README.md index 0dc88df2..3a644af9 100644 --- a/README.md +++ b/README.md @@ -133,6 +133,16 @@ To use partials in mustache you will need to pass the names and paths in the opt } ``` +To use partials in handlebars you will need to pass the names and paths in the options parameter: +```js + options: { + partials: { + header: 'header.hbs', + footer: 'footer.hbs' + } + } +``` + To configure nunjunks environment after initialisation, you can pass callback function to options: ```js options: { diff --git a/index.js b/index.js index 1eed1251..722d3217 100644 --- a/index.js +++ b/index.js @@ -116,6 +116,7 @@ function fastifyView (fastify, opts, next) { } let error = null + const partialsHtml = {} Object.keys(partials).map((key, index) => { readFile(join(templatesDir, partials[key]), 'utf-8', (err, data) => { if (err) { @@ -124,10 +125,11 @@ function fastifyView (fastify, opts, next) { if (options.useHtmlMinifier && (typeof options.useHtmlMinifier.minify === 'function')) { data = options.useHtmlMinifier.minify(data, options.htmlMinifierOptions || {}) } - partials[key] = data + + partialsHtml[key] = data if (--filesToLoad === 0) { - lru.set(`${page}-Partials`, partials) - callback(error, partials) + lru.set(`${page}-Partials`, partialsHtml) + callback(error, partialsHtml) } }) }) @@ -305,21 +307,35 @@ function fastifyView (fastify, opts, next) { return } + const options = Object.assign({}, opts.options) data = Object.assign({}, defaultCtx, data) // append view extension page = getPage(page, 'hbs') + getTemplateString(page, (err, templateString) => { + if (err) { + this.send(err) + return + } - const toHtml = lru.get(page) + getPartials(page, options.partials || {}, (err, partialsObject) => { + if (err) { + this.send(err) + return + } - if (toHtml && prod) { - if (!this.getHeader('content-type')) { - this.header('Content-Type', 'text/html; charset=' + charset) - } - this.send(toHtml(data)) - return - } + Object.keys(partialsObject).forEach((name) => { + engine.registerPartial(name, engine.compile(partialsObject[name])) + }) - readFile(join(templatesDir, page), 'utf8', readCallback(this, page, data)) + const template = engine.compile(templateString) + const html = template(data) + + if (!this.getHeader('content-type')) { + this.header('Content-Type', 'text/html; charset=' + charset) + } + this.send(html) + }) + }) } function viewMustache (page, data, opts) { @@ -343,7 +359,11 @@ function fastifyView (fastify, opts, next) { return } let html = engine.render(templateString, data, partialsObject) - this.header('Content-Type', 'text/html; charset=' + charset).send(html) + + if (!this.getHeader('content-type')) { + this.header('Content-Type', 'text/html; charset=' + charset) + } + this.send(html) }) }) } diff --git a/templates/body.hbs b/templates/body.hbs new file mode 100644 index 00000000..1eeeb059 --- /dev/null +++ b/templates/body.hbs @@ -0,0 +1 @@ +
{{ text }}
diff --git a/templates/index-with-partials.hbs b/templates/index-with-partials.hbs new file mode 100644 index 00000000..ea591ffe --- /dev/null +++ b/templates/index-with-partials.hbs @@ -0,0 +1,7 @@ + + + + + {{> body }} + + diff --git a/test/test-handlebars.js b/test/test-handlebars.js index 50da00e9..98e348a0 100644 --- a/test/test-handlebars.js +++ b/test/test-handlebars.js @@ -440,3 +440,38 @@ test('fastify.view with handlebars engine and callback in production mode', t => }) }) }) + +test('reply.view with handlebars engine with partials', t => { + t.plan(6) + const fastify = Fastify() + const handlebars = require('handlebars') + const data = { text: 'text' } + + fastify.register(require('../index'), { + engine: { + handlebars: handlebars + }, + options: { + partials: { 'body': './templates/body.hbs' } + } + }) + + fastify.get('/', (req, reply) => { + reply.view('./templates/index-with-partials.hbs', data) + }) + + fastify.listen(0, err => { + t.error(err) + sget({ + method: 'GET', + url: 'http://localhost:' + fastify.server.address().port + }, (err, response, replyBody) => { + t.error(err) + t.strictEqual(response.statusCode, 200) + t.strictEqual(response.headers['content-length'], '' + replyBody.length) + t.strictEqual(response.headers['content-type'], 'text/html; charset=utf-8') + t.strictEqual(handlebars.compile(fs.readFileSync('./templates/index-with-partials.hbs', 'utf8'))(data), replyBody.toString()) + fastify.close() + }) + }) +})