From 3bc267a0577431596a303f51b59821f7629fa23c Mon Sep 17 00:00:00 2001 From: Jason Williams Date: Sat, 5 Oct 2019 18:06:33 -0600 Subject: [PATCH] Video 49: Advanced Templating --- web-server/src/app.js | 11 ++++++++--- web-server/templates/partials/footer.hbs | 1 + web-server/templates/partials/header.hbs | 7 +++++++ web-server/templates/{ => views}/about.hbs | 4 ++-- web-server/templates/{ => views}/help.hbs | 3 ++- web-server/templates/{ => views}/index.hbs | 4 ++-- 6 files changed, 22 insertions(+), 8 deletions(-) create mode 100644 web-server/templates/partials/footer.hbs create mode 100644 web-server/templates/partials/header.hbs rename web-server/templates/{ => views}/about.hbs (72%) rename web-server/templates/{ => views}/help.hbs (81%) rename web-server/templates/{ => views}/index.hbs (74%) diff --git a/web-server/src/app.js b/web-server/src/app.js index b9ec236..6d5adaf 100644 --- a/web-server/src/app.js +++ b/web-server/src/app.js @@ -1,22 +1,25 @@ const path = require('path') const express = require('express') +const hbs = require('hbs') const app = express() // Define paths for Express config const publicDirectoryPath = path.join(__dirname, '../public') -const viewsPath = path.join(__dirname, '../templates') +const viewsPath = path.join(__dirname, '../templates/views') +const partialsPath = path.join(__dirname, '../templates/partials') // Setup handlebars engine and views location app.set('view engine', 'hbs') app.set('views', viewsPath) +hbs.registerPartials(partialsPath) // Setup static directory to serve app.use(express.static(publicDirectoryPath)) app.get('', (req, res) => { res.render('index', { - title: 'Weather App', + title: 'Weather', name: 'Jason Williams' }) }) @@ -30,7 +33,9 @@ app.get('/about', (req, res) => { app.get('/help', (req, res) => { res.render('help', { - helpText: 'This is some helpful text.' + helpText: 'This is some helpful text.', + title: 'Help', + name: 'Jason Williams' }) }) diff --git a/web-server/templates/partials/footer.hbs b/web-server/templates/partials/footer.hbs new file mode 100644 index 0000000..78cd476 --- /dev/null +++ b/web-server/templates/partials/footer.hbs @@ -0,0 +1 @@ +

Created by {{name}}

diff --git a/web-server/templates/partials/header.hbs b/web-server/templates/partials/header.hbs new file mode 100644 index 0000000..2a7d7ad --- /dev/null +++ b/web-server/templates/partials/header.hbs @@ -0,0 +1,7 @@ +

{{title}}

+ +
+ Weather + About + Help +
diff --git a/web-server/templates/about.hbs b/web-server/templates/views/about.hbs similarity index 72% rename from web-server/templates/about.hbs rename to web-server/templates/views/about.hbs index ce55822..bac94cc 100644 --- a/web-server/templates/about.hbs +++ b/web-server/templates/views/about.hbs @@ -7,9 +7,9 @@ -

{{title}}

+ {{>header}} -

Created by {{name}}

+ {{>footer}} diff --git a/web-server/templates/help.hbs b/web-server/templates/views/help.hbs similarity index 81% rename from web-server/templates/help.hbs rename to web-server/templates/views/help.hbs index 6f81527..745bbc2 100644 --- a/web-server/templates/help.hbs +++ b/web-server/templates/views/help.hbs @@ -7,8 +7,9 @@ -

Help

+ {{>header}}

{{helpText}}

+ {{>footer}} diff --git a/web-server/templates/index.hbs b/web-server/templates/views/index.hbs similarity index 74% rename from web-server/templates/index.hbs rename to web-server/templates/views/index.hbs index d4da206..b71b29a 100644 --- a/web-server/templates/index.hbs +++ b/web-server/templates/views/index.hbs @@ -8,8 +8,8 @@ -

{{title}}

-

Created by {{name}}

+ {{>header}} + {{>footer}}