Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js + Express + Handlebars.js + partial views

I am trying to make a simple HelloWorld project with Node.js|Express using Handlebars.js as a server template engine.

The problem is that I couldn't find any examples of using such chain, especially with multiple view.

For example I would like to define header view:

<header>   <span>Hello: {{username}}</span> </header> 

And use it in every page with other views.

Maybe I am thinking about this views in a wrong way, I thought that view is kind of control that I can reuse on any page inside any other view.

I appreciate any link to the tutorial or (much better) open source project that I can lear from.

like image 618
Artem Yarulin Avatar asked May 05 '13 13:05

Artem Yarulin


People also ask

How do partials work in Handlebars?

Partial Blocks If failover is desired instead, partials may be called using the block syntax. Which will render Failover content if the myPartial partial is not registered. When called in this manner, the block will execute under the context of the partial at the time of the call.

How do you include partials in HBS?

hbs' files: To get the reference to these files to work we need to registers these files, which are partials using: '__dirname' is the root folder of our project and link to the subfolder '/views/partials' as seen here: Our code in our 'home.

Why would we use partials in a Handlebars template?

Partials are a common templating concept not unique to Handlebars. The idea behind it is to create templates that are likely to be re-used, separate them into their own file (a Partial), and then use them in different templates. You may think at Partials as a simple a tool to modularize your templates.


1 Answers

I know this had been asked a long time ago, but no one has shown an answer in this post. So I will do so here. To ensure everyone is on the same page, I will be verbose in my answer. I apologize in advance if it seems overly simplistic.

In your server.js file (or app.js, wherever you defined handlebars as your view engine). Depending on what you are using as your npm package, such as hbs or express-handlebars etc. it may look different, but similar to this. Note: I'm using express-handlebars in this example.

file: server.js

... var express     = require( 'express'),     hbs         = require( 'express-handlebars' ),     app         = express(); ...  app.engine( 'hbs', hbs( {    extname: 'hbs',    defaultLayout: 'main',    layoutsDir: __dirname + '/views/layouts/',   partialsDir: __dirname + '/views/partials/' } ) );  app.set( 'view engine', 'hbs' );  ... 

and your file structure should look something like this:

| /views/    |--- /layouts/ |----- main.hbs |--- /partials/ |----- header.hbs |----- footer.hbs |----- ... etc. |--- index.hbs | server.js 

And your main.hbs file should look like this:

file: main.hbs

... {{> header }} ... <span> various other stuff </span> ... {{> footer }} 

To denote a partial you use this syntax: {{> partialsNames }}.

like image 87
Tristan Isfeld Avatar answered Sep 27 '22 23:09

Tristan Isfeld