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.

Trying to get this to work with Express

See original GitHub issue

I saw there was another closed issue about this but it didn’t really help me. I have a frontend framework that’s currently using this plugin with gulp to compile my templates into HTML but now I’m looking to move to an express server and for the server to serve the templates as responses instead.

Here’s a simplified version of what I’m working with:

Directories:

- app.js
- build
- src
    - layouts
        - main.hbs
        - generic.hbs
    - partials
        - header.hbs
    - pages
        - index.hbs
    - styles
    - scripts
    - images

app.js

let express = require("express");
let exphbs = require("express-handlebars");
let layouts = require("handlebars-layouts");

let app = express();

let hbs = exphbs.create({
	extname: ".hbs",
	layoutsDir: "src/layouts/",
	defaultLayout: "main",
	partialsDir: ["src/partials/"]
});

var handlebars = hbs.handlebars;

handlebars.registerHelper(layouts(handlebars));

app.engine(".hbs", hbs.engine);
app.set("view engine", ".hbs");
app.set("views", "src/");

app.get("/", function (req, res) {
	res.render("pages/index");
});

app.listen(3000, function(){
	console.log("Now listening on port 3000");
});

index.hbs

{{#extend "generic"}}
	{{#content "content"}}
		Hello world
	{{/content}}
{{/extend}}

generic.hbs

{{#extend "main"}}
	{{#content "body"}}
		<div class="padded-h-xl padded-v-md">
			{{#block "content"}}{{/block}}
		</div>
	{{/content}}
{{/extend}}

And the error I get:

Error: Missing partial: 'generic'

No idea if I’m going about this the right way or not. I tried searching for “express-handlebars” and “handlebars-layouts” to try and find some code examples of them both being used together but couldn’t find anything useful.

Any ideas?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
Jazcashcommented, Feb 21, 2017

Got it working with handlebars-wax. Cheers. Here’s my code for future reference:

let express = require("express");
let handlebars = require("handlebars");
let layouts = require("handlebars-layouts");
let handlebarsWax = require('handlebars-wax');

let app = express();

var wax = handlebarsWax(handlebars)
	.partials('src/partials/**/*.{hbs,js}')
	.partials('src/layouts/**/*.{hbs,js}')
	.helpers(layouts);

app.engine("hbs", wax.engine);
app.set("view engine", "hbs");
app.set("views", "src/");

app.get("/", function (req, res) {
	res.render("pages/index");
});

app.listen(3000, function(){
	console.log("Now listening on port 3000");
});
0reactions
shannonmoellercommented, Mar 4, 2017

Got it working with handlebars-wax.

Yay!

Please note that the layout helpers expect the partials to be registered directly, you can’t use express-handlebars’ dynamic partial loading.

^^^ from #20

Read more comments on GitHub >

github_iconTop Results From Across the Web

node.js - NodeJS w/Express Error: Cannot GET - Stack Overflow
static . It needs to be a directory. The app.get('/'.... will be responsible to render that file accordingly.
Read more >
Error handling - Express.js
Errors that occur in synchronous code inside route handlers and middleware require no extra work. If synchronous code throws an error, then Express...
Read more >
How To Get Started with Node.js and Express - DigitalOcean
Learn how to use the Express framework in Node to create a web server. ... Get $200 to try DigitalOcean - and do...
Read more >
Express/Node introduction - Learn web development | MDN
Express is the most popular Node web framework, and is the underlying library for a number of other popular Node web frameworks. It...
Read more >
LEARN EXPRESS JS IN 15 MINUTES! - YouTube
Express JS is an awesome opinionated framework for Node.js that helps ... Link - https:// go.tech/erik Use Code erik.tech at Checkout for a ......
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