queckezz/koa-views

Strange issue with partials in handlebars

k-dahl opened this issue · 2 comments

I'm using almost the exact code you posted in another issue for partials with handle bars:

const Koa = require('koa')
const views = require('koa-views')
const app = new Koa()

app.use(views(__dirname, {
    map: {hbs: 'handlebars'},
    options: {
        partials: {
            subTitle: './my-partial' // requires ./my-partial.hbs
        }
    }
}))

app.use((ctx) => {
    ctx.state = {title: 'my title', author: 'queckezz'}
    return ctx.render('./my-view.hbs')
})

app.listen(8083, () => {
    console.log('Server listening on port 8083')
})

On first render this works... on second, I get an error:

Error: ENOENT: no such file or directory, open '/home/blitzd/WebstormProjects/untitled/<div>Partial Test</div>.hbs'

Instead of trying to load the partial by the filename configured in the code example, it's actually looking for the file with the name being the actual CONTENT from the partial, but this only happens on subsequent requests and never the first.

My main content:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
{{> subTitle }}
</body>
</html>

My partial:

<div>Partial Test</div>

Debug output for first and second load:

  koa-views render `./my-view.hbs` with {"partials":{"subTitle":"./my-partial"},"title":"my title","author":"queckezz"} +0ms
  koa-views render `./my-view.hbs` with {"partials":{"subTitle":"<div>Partial Test</div>"},"title":"my title","author":"queckezz"} +6s

This issue is about the same thing: #77

Haven't gotten an answer yet, I've moved on to using koa-hbs instead