Templating engines met Handlebars
Handlebars is a simple templating language based on {{mustache}}. It uses a template and an input object to generate HTML or other text formats. Handlebars templates look like regular text with embedded Handlebars expressions.
Installeer handlebars
npm install handlebars
Installeer express-handlebars
npm install express-handlebars
<!DOCTYPE html>
<html lang="nl">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Mijn eerste handlebars applicatie</title>
</head>
<body>
{{{body}}}
</body>
</html>
import { create } from "express-handlebars";
const hbs = create({
extname: "hbs",
});
app.engine("hbs", hbs.engine);
app.set("view engine", "hbs");
app.set("views", path.join(SOURCE_PATH, "views"));
import * as path from "path";
export const SOURCE_PATH = path.resolve("src");
export const home = (req, res) => {
res.render('home');
}
export const home = (req, res) => {
res.render('home');
}
export const home = (req, res) => {
res.render('home');
}
app.get('/', home);
Templating engines met Handlebars
Handlebars expressions are the basic unit of a Handlebars template. You can use them alone in a {{mustache}}, pass them to a Handlebars helper, or use them as values in hash arguments.
<p>{{firstname}}</p>
{
person: {
firstname: "Yehuda",
lastname: "Katz",
},
}
{{person.firstname}} {{person.lastname}}
{{#with person}}
{{firstname}} {{lastname}}
{{/with}}
<ul class="people_list">
{{#each people}}
<li>{{this}}</li>
{{/each}}
</ul>
Templating engines met Handlebars
Handlebars allows for template reuse through partials. Partials are normal Handlebars templates that may be called directly by other templates.
{{> myPartial }}
{{> myPartial myOtherContext }}
Templating engines met Handlebars
In onze app vind je afbeeldingen van dinosaurussen. Je kunt routes gebruiken om naar een overzicht van dinosaurussen te gaan en vervolgens door te klikken naar een detailpagina.
Een scenario, waar een aparte DinoController aangewezen is.
We hebben ook gedeelde data die in beide pagina's nodig is.
export const index = (req, res) => {};
export const detail = (req, res) => {};
Templating engines met Handlebars
<div class="entry">
{{#if author}}
<h1>{{firstName}} {{lastName}}</h1>
{{/if}}
</div>
{
author: true,
firstName: "Yehuda",
lastName: "Katz",
}
<div class="entry">
<h1>Yehuda Katz</h1>
</div>
<div class="entry">
{{#unless license}}
<h3 class="warning">
WARNING: This entry does not have a license!
</h3>
{{/unless}}
</div>
Itereren over een lijst doe je met {{#each}}
<ul class="people_list">
{{#each people}}
<li>{{this}}</li>
{{/each}}
</ul>
{
people: [
"Yehuda Katz",
"Alan Johnson",
"Charles Jolley",
],
}
<ul class="people_list">
<li>Yehuda Katz</li>
<li>Alan Johnson</li>
<li>Charles Jolley</li>
</ul>
{{#with person}}
{{firstname}} {{lastname}}
{{/with}}
{
person: {
firstname: "Yehuda",
lastname: "Katz",
},
}
Output: Yehunda Katz
Templating engines met Handlebars
Block helpers make it possible to define custom iterators and other functionality that can invoke the passed block with a new context.
const hbs = create({
helpers: HandlebarsHelpers,
extname: "hbs",
});
<h1>
Welcome to handlebars, {{#bold}}{{firstname}}!{{/bold}}
</h1>
import handlebars from "handlebars";
const { SafeString } = handlebars;
export default {
bold: function(options) {
return new SafeString(`<strong>${options.fn()}</strong>`);
}
};
HandleBarsHelpers.js
De options.fn() functie gedraagt zich als een gecompileerde versie van de Handlebars template. De functie neemt als parameter een context mee.
import handlebars from "handlebars";
const { SafeString } = handlebars;
export default {
bold: function(options) {
return new SafeString(
`<strong>${options.fn({ firstname: "Eveline"})}</strong>`
);
}
};
HandleBarsHelpers.js
Resulteert in
import handlebars from "handlebars";
const { SafeString } = handlebars;
export default {
bold: function(context, options) {
return new SafeString(
`<strong>${context} ${options.fn({ firstname: "Eveline"})}</strong>`
);
}
};
HandleBarsHelpers.js
Text
<h1>
Welcome to handlebars, {{#bold "Mrs."}}{{firstname}}!{{/bold}}
</h1>
Resulteert in
loud: (text) => text.toUpperCase()
<h1>
Welcome to {{loud "handlebars"}},
{{#bold "Mrs."}}{{firstname}}!{{/bold}}
</h1>
{{#commentList comments}}
<li>{{title}}</li>
{{/commentList}}