0

I have a template which has a header, footer, and sidebar; I want to separate them into separate files and include them in the main template. previously I was using PHP, and it was very easy to do this by include() or required() function, but since I am using node.js instead of PHP I don't know how to do that.

M.A.O.2
  • 37
  • 9

3 Answers3

2
<script>
 $(document).ready(function (e) {
$( "#header" ).load( "header.html" );
$( "#sidebar" ).load( "sidebar.html" );
$( "#footer" ).load( "footer.html" );

});
</script>

You can use jquery for that.

Supun Abesekara
  • 708
  • 7
  • 32
0

I guess doing something like

<% include ../partials/head %> 

in your view should work just fine. You can also have a look here for more info

dstrants
  • 7,423
  • 2
  • 19
  • 27
0

You can use W3schools library to achieve this easily.

More info here

add this script in your HTML

<script src="https://www.w3schools.com/lib/w3.js"></script>

Call w3.includeHTML() to include the HTML:

<script>
w3.includeHTML();
</script>

full example

<!DOCTYPE html>
<html>
<script src="https://www.w3schools.com/lib/w3.js"></script>

<body>

<div w3-include-html="content.html"></div> 

<script>
w3.includeHTML();
</script>

</body>
</html>
Imran Rafique
  • 1,986
  • 2
  • 12
  • 14