Skip to content

Instantly share code, notes, and snippets.

@belaz
Forked from eliOcs/apps.js
Created August 18, 2018 17:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save belaz/997a6bb16d722f99b47505759970b208 to your computer and use it in GitHub Desktop.
Save belaz/997a6bb16d722f99b47505759970b208 to your computer and use it in GitHub Desktop.
Express and Handlebars
/*jslint node: true */
"use strict";
var express = require("express"),
consolidate = require("consolidate"),
Handlebars = require("handlebars"),
fs = require("fs");
var app = express();
// Configure the Handlebars engine
app.engine("html", consolidate.handlebars);
app.set("view engine", "html");
app.set("views", __dirname + "/views");
// Register partials
var partials = "./views/partials/";
fs.readdirSync(partials).forEach(function (file) {
var source = fs.readFileSync(partials + file, "utf8"),
partial = /(.+)\.html/.exec(file).pop();
Handlebars.registerPartial(partial, source);
});
// Define a route that renders the index view
app.get("/", function (req, res) {
res.render("index", {
hello: "Hello",
world: "World"
});
});
app.listen(3000);
{
"name": "express-and-handlebars",
"version": "0.0.0",
"main": "app.js",
"dependencies": {
"express": "3.x",
"consolidate": "0.4.0",
"handlebars": "1.0.7"
}
}
<html>
<head>
<title>Index</title>
</head>
<body>
<p>{{ hello }}</p>
{{> partial }}
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment