Skip to content

Commit

Permalink
fix: stop duplication of path in template dir (#440)
Browse files Browse the repository at this point in the history
Closes #437

When the `root` option is passed explicitly and the `art-template` engine is in use, the `root` value is being duplicated in the resolved template directory.

This leads to an error like this: notice how `templates` is present twice in the path.

```
  -{"statusCode":500,"error":"Internal Server Error","message":"template not found: ENOENT: no such file or directory, open
  '/home/rich/point-of-view/templates/templates/index.art'"}
```

The fix is to remove this duplication. The `resolveTemplateDir` function already uses any `root` option (if present) so there's no need to consider it again.
  • Loading branch information
richdouglasevans authored Dec 23, 2024
1 parent 568543a commit 9da71c4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ async function fastifyView (fastify, opts) {
function render (filename, data) {
let render
if (typeof filename === 'string') {
confs.filename = join(templatesDir, filename)
confs.filename = filename
render = engine.compile(confs)
} else if (typeof filename === 'function') {
render = filename
Expand Down
37 changes: 37 additions & 0 deletions test/test-art-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,43 @@ test('reply.view with art-template engine and custom templates folder', t => {
})
})

test('reply.view with art-template engine and explicit root folder', t => {
t.plan(6)
const fastify = Fastify()
const art = require('art-template')
const data = { text: 'text' }

fastify.register(require('../index'), {
engine: {
'art-template': art
},
root: 'templates'
})

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

fastify.listen({ port: 10086 }, err => {
t.error(err)

sget({
method: 'GET',
url: 'http://127.0.0.1:10086/'
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.equal(response.headers['content-length'], '' + body.length)
t.equal(response.headers['content-type'], 'text/html; charset=utf-8')

const templatePath = path.join(__dirname, '..', 'templates', 'index.art')

t.equal(art(templatePath, data), body.toString())
fastify.close()
})
})
})

test('reply.view for art-template without data-parameter and defaultContext', t => {
t.plan(6)
const fastify = Fastify()
Expand Down

0 comments on commit 9da71c4

Please sign in to comment.