-1

So I have a website with multiple webpages and they have the same head and footer and the only thing I'm changing is the content. I know I can use php and use the include, but I am trying to avoid that.

Is there another way to plug in the head and footer without using PHP?

Jake
  • 193
  • 1
  • 5
  • 15

3 Answers3

0

There isn't currently a way to do this in straight HTML (there is a HTML5 import feature coming, but current support is lousy), so you need to use some sort of template system to achieve this. There are a lot of options out there such as Handlebars, Liquid, or Jade. They will allow you to write partials and include them, then compile them to individual html files for deployment using gulp, grunt, or other task runners. You might also look at static site generators such as Jekyll, middleman, or assemble.io as these are the sort of features they offer without having to run a sever side language when you launch your site.

JustH
  • 1,202
  • 7
  • 8
0

Here is a JS way to do it: (with code taken directly from this answer):

Create a javascript file: footerheader.js Give it the content:

document.getElementById("myHead").innerHTML =
    "<span id='headerText'>Title</span>"
    + "<span id='headerSubtext'>Subtitle</span>";

document.getElementById("myFooter").innerHTML =
    "<p id='copyright'>Copyright &copy; " + new Date().getFullYear() + " You. All"
    + " rights reserved.</p>"
    + "<p id='credits'>Layout by You</p>"
    + "<p id='contact'><a href='mailto:you@you.com'>Contact Us</a> / "
    + "<a href='mailto:you@you.com'>Report a problem.</a></p>";

All this code, you can replace with your own names and ids.

Then your html:

<header id="myHead"></header>
<h3>content</h3>
<footer id="myFooter"></footer>

<script src="footerheader.js"></script>

With the same ids across all webpages, and the same script included, you can achieve this.

Community
  • 1
  • 1
Arnav Borborah
  • 9,956
  • 4
  • 32
  • 69
-1

The best method to do that is use the method require_once(), is a simple way but work's fine to do that think's.

Regard's.

Radames E. Hernandez
  • 3,261
  • 20
  • 29