Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Including an html file into another html file using Handlebars.js

In PHP, it's easy to include a file in another one to avoid redundancy of the code using the include keyword. Is there a similar solution in Node.JS using Handlebars.JS?

like image 919
Pierre Avatar asked Jun 06 '15 11:06

Pierre


People also ask

How do you add templates in your HTML in handlebars?

Templates The recommended way of adding templates to your page is by including them in <script> tags with a special type. The type attribute is important, otherwise the browser will attempt to parse them as JavaScript (which they are not). The templates have an easy to grasp syntax.

Which is better EJS or handlebars?

EJS is way faster than Jade and handlebars. EJS has a really smart error handling mechanism built right into it. It points out to you, the line numbers on which an error has occurred so that you don't end up looking through the whole template file wasting your time in searching for bugs.


1 Answers

From your question it sounds like you are looking for handlebars partials. For an example check out https://github.com/donpark/hbs/tree/master/examples/partial.

In short, you'd have something which looked like:

index.hbs:

<!DOCTYPE HTML>
<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Your Website</title>
</head>

<body>
{{> header}}
</body>
</html>

where {{> header}} is referencing the header.hbs partial.

like image 79
Nick Bartlett Avatar answered Oct 05 '22 20:10

Nick Bartlett