shannonmoeller / handlebars-layouts

Handlebars helpers which implement layout blocks similar to Jinja, Nunjucks (Swig), Pug (Jade), and Twig.
http://npm.im/handlebars-layouts
MIT License
361 stars 29 forks source link

Extending partial causes error (handlebars.compile is not a function) #46

Closed martyan closed 4 years ago

martyan commented 5 years ago

Trying to implement handlebars-layouts, I get error when extending a partial: ERROR in Template execution failed: TypeError: handlebars.compile is not a function

  1. I registered the helpers and partials (handlebars.js):
    
    const Handlebars = require('handlebars/runtime')
    const layouts = require('handlebars-layouts')
    const fs = require('fs')

Handlebars.registerHelper(layouts(Handlebars))

const partial = fs.readFileSync('./src/components/partial.hbs', 'utf-8') Handlebars.registerPartial('partial', partial)

module.exports = Handlebars

<br />

2. Added the handlebars runtime to handlebars-loader options in webpack config ([webpack.config.js](https://github.com/martyan/start-coding/blob/handlebars-layouts/webpack.config.js#L37)):

/ ... / test: /.hbs$/, loader: 'handlebars-loader', options: { runtime: SRC_PATH + '/helpers/handlebars', helperDirs: [SRC_PATH + '/helpers'] } / ... /

<br />

3. Partial component [partial.hbs](https://github.com/martyan/start-coding/blob/handlebars-layouts/src/components/partial.hbs)
4. Extending partial [webpack-layouts.hbs](https://github.com/martyan/start-coding/blob/handlebars-layouts/src/pages/webpack-layouts.hbs)
<br />
<br />

What am I missing?
When I register the partial as follows, no error is appearing, but the partial is not extended ([handlebars.js](https://github.com/martyan/start-coding/blob/handlebars-layouts/src/helpers/handlebars.js#L9))

Handlebars.registerPartial('partial', () => { return partial })


<br />
<br />

Repro repository:
https://github.com/martyan/start-coding/tree/handlebars-layouts

Commit with `handlebars-layouts` implementation:
https://github.com/martyan/start-coding/commit/ad7583023342c73f41fd90a1cfc4df30677f9b58
Paillat-dev commented 5 months ago

What was the solution to this?