question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Get the handlebar instance

See original GitHub issue

I’m trying to extend handlebars with helpers from https://github.com/shannonmoeller/handlebars-layouts. To register the helpers i need access to the underlying handlebars instance of express-handlebars. How can i get the instance?

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:2
  • Comments:5

github_iconTop GitHub Comments

3reactions
ghostcommented, Apr 10, 2019

The code above allows handlebars-layouts to register, but doesn’t allow it to work with express-handlebars.

Calling {{#extend "layouts/foo"}} fails with the error

Error: Missing partial: 'layouts/foo'
    at Object.extend (/app/node_modules/handlebars-layouts/index.js:120:11)
    at Object.eval [as main] (eval at createFunctionContext (/app/node_modules/handlebars/dist/cjs/handlebars/compiler/javascript-compiler.js:257:23), <anonymous>:5:92)
    at main (/app/node_modules/handlebars/dist/cjs/handlebars/runtime.js:175:32)
    at ret (/app/node_modules/handlebars/dist/cjs/handlebars/runtime.js:178:12)
    at ret (/app/node_modules/handlebars/dist/cjs/handlebars/compiler/compiler.js:526:21)
    at ExpressHandlebars._renderTemplate (/app/node_modules/express-handlebars/lib/express-handlebars.js:247:12)
    at ExpressHandlebars.<anonymous> (/app/node_modules/express-handlebars/lib/express-handlebars.js:173:21)

This is how I’m setting the two up

const path = require('path')
const express = require('express')
const exphbs = require('express-handlebars')
const layouts = require('handlebars-layouts')
const handlerbarsHelpers = require('./handlebars-helpers')

const app = express()

const hbs = exphbs.create({
  helpers: handlerbarsHelpers,
  extname: 'hbs',
  partialsDir: path.join(__dirname, 'views', 'partials')
})

hbs.getPartials().then(partials => console.log(Object.keys(partials)))

app.engine('.hbs', hbs.engine)
app.set('view engine', '.hbs')
app.set('views', path.join(__dirname, 'views'))

layouts.register(hbs.handlebars)

app.get('/foo', (req, res) => {
  res.render('foo')
})
1reaction
ghostcommented, Apr 10, 2019

I can fix the issue by manually registering the partial with handlebars. Is there a way to avoid that?

hbs.getPartials().then(partials => hbs.handlebars.registerPartial('layouts/foo', partials['layouts/foo']))
Read more comments on GitHub >

github_iconTop Results From Across the Web

Handlebars
Get Started → ... Handlebars provides the power necessary to let you build semantic templates ... Handlebars is largely compatible with Mustache templates....
Read more >
Getting Started with Handlebars.js - Sabe.io
Use the templating engine library Handlebars.js to build semantic templates, inject context data, and utilize helpers.
Read more >
A Beginner's Guide to Handlebars - SitePoint
In your JavaScript file we firstly need to retrieve the template from the HTML document. In the following example, we'll use the ID...
Read more >
How can I get an instance of a model's controller inside a ...
I've got a sticky situation that I keep on running into: The need for a new instance of a controller inside a handlebars...
Read more >
Handlebars Tutorial - YouTube
Get the Code Here : http://goo.gl/h81zZAIn this video we'll learn Handlebars by walking through numerous examples. Handlebars is a superset ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found