Trying to get this to work with Express
See original GitHub issueI 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:
- Created 7 years ago
- Comments:5 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Got it working with
handlebars-wax
. Cheers. Here’s my code for future reference:Yay!
^^^ from #20