Skip to content

Instantly share code, notes, and snippets.

@mucahitnezir
Last active January 13, 2018 21:38
Show Gist options
  • Save mucahitnezir/7758c792f6a8cefe6bd815170f57e98f to your computer and use it in GitHub Desktop.
Save mucahitnezir/7758c792f6a8cefe6bd815170f57e98f to your computer and use it in GitHub Desktop.
My custom handlebars template engine configuration
const express = require("express")
, exphbs = require("express-handlebars");
/* Create express instance */
var app = express();
/* Configure hbs view engine */
var hbs = exphbs({
extname: 'hbs',
defaultLayout: 'mainLayout',
layoutsDir: __dirname + '/views/layouts/',
helpers: {
section: function(name, options) {
if (!this._sections)
this._sections = {};
this._sections[name] = options.fn(this);
return null;
}
}
});
/* Set view engine to express app */
app.engine('hbs', hbs);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');
/* Render a page */
app.get('/', function() {
res.render('homepage');
});
/* Listen app */
var port = 3000;
app.listen(port, function() {
console.log('Your app is ready at ' + port + ' port');
});
{{#section 'head'}}
<!-- stuff that goes in head...example: -->
<meta name="robots" content="noindex">
{{/section}}
<h1>Body Blah Blah</h1>
<p>This goes in page body.</p>
<html>
<head>
{{{_sections.head}}}
</head>
<body>
{{{body}}}
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment