5.5 Advanced Templating

Partial
A piece of code that may be reused as one part of another hbs.

hbs.registerPartials(__dirname + '/views/partials')


Setup Partials
Create a folder called Partials.
In that folder, we create footer.hbs which contains

<footer>
<p>Copyright {{currentYear}}</p>
</footer>

And we now can use this partial by
{{> footer}}

Example

<body>
<h1>{{pageTitle}}</h1>
<p>{{welcomeMessage}}</p>
{{> footer}}
</body>

To allow nodemon refresh according to more files
nodemon server.js -e js,hbs
-e: extensions


HBS Helper

hbs.registerHelper('getCurrentYear', ()=>{
return new Date().getFullYear();
})

Now, to use this helper

<footer>
<p>Created by Jude Gao - Copyright {{getCurrentYear}}</p>
</footer>

Note: HBS will first search for helpers. If not found, it will then search for injections.


Helpers with Argument Passing

hbs.registerHelper('screamIt', (text)=>{
return text.toUpperCase();
})

<p>{{screamIt welcomeMessage}}</p>