Book Image

Mastering Web Application Development with Express

By : Alexandru Vladutu
Book Image

Mastering Web Application Development with Express

By: Alexandru Vladutu

Overview of this book

Table of Contents (18 chapters)
Mastering Web Application Development with Express
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Sharing code between templates with partial views


Partial views (more commonly known as partials) allow us to define commonly shared parts of a web page (for example, the header and the footer) that can be reused in multiple views.

Partials have been removed from Express since Version 3, and they are now template-specific, meaning it's the decision of the template engine to provide them or not.

Let's consider a sample application that has multiple pages but each one shares the header and the footer. For starters, we are going to create this application using EJS and then achieve the same with Jade.

The server-ejs.js file will initialize everything and render index.html as the main page of the application, as shown in the following code:

var express = require('express');
var ejs = require('ejs');
var app = express();

// assign the ejs engine to .html files
app.engine('html', ejs.renderFile);

// set .html as the default extension
app.set('view engine', 'html');
app.set('views', __dirname + ...