1

I'm working on a "plain" HTML-CSS website. It contains several pages. I want some code to be included in all HTML pages. (All have a navbar at the top)

What is the best way to include that part of the code in all files in order to avoid repeating code?

I've tried creating a directory named "partials", there I added a file called header.html and then tried including it in a different file with PHP. like so:

<?php include "./partials/header.html" ?>

This is not working. Please advise me what to do.

Thanks!

  • 1
    _..I'm working on a "plain" HTML-CSS website.._ Why are you expecting php to work in a html file? – B001ᛦ Aug 13 '20 at 10:33
  • Does this answer your question? [Include another HTML file in a HTML file](https://stackoverflow.com/questions/8988855/include-another-html-file-in-a-html-file) – Vishnu Bhadoriya Aug 13 '20 at 10:34
  • @B001ᛦ The home page though is rendered using index.php which contains: So you could call it a "php" project – Ori Silberman Aug 13 '20 at 10:35
  • _..The home page though is rendered using index.php..._ well then it is not plain html css. Check the path to the html file or look at @VishnuBhadoriya duplicate suggestion – B001ᛦ Aug 13 '20 at 10:37
  • _...which contains: – B001ᛦ Aug 13 '20 at 10:37
  • @VishnuBhadoriya I've seen this, it doesn't answer my question unfortunately. I didn't understand it. Someone said it only works on a server and not on a local machine. – Ori Silberman Aug 13 '20 at 10:37
  • @B001ᛦ This is not my issue. It's working. – Ori Silberman Aug 13 '20 at 10:38
  • 1
    @B001ᛦ maybe he is using **** inside a .html file :D – Kebab Programmer Aug 13 '20 at 10:42
  • a normal `` would work – Kebab Programmer Aug 13 '20 at 10:43
  • @KebabProgrammer Hey, I've tried doing it with PHP inside html. Exactly. But it didn't work :( – Ori Silberman Aug 13 '20 at 10:47
  • are all your files ending with a `.php`? If they aren't then its probably why your include isn't working – Kebab Programmer Aug 13 '20 at 10:48
  • @KebabProgrammer They're not ending with a .php because they are HTML files. Do I have to change all html file endings to .php? – Ori Silberman Aug 13 '20 at 10:50
  • The browser needs to know that it's intepreting a php file, so that any php markup inside the file will be intepreted correctly. if you have your `` inside a `.html` file, it will get ignored. and about 3 comments up, i posted how to properly include a file – Kebab Programmer Aug 13 '20 at 10:51
  • @OriSilberman you can copy over the contents, eg `header.html` into `header.php`, this way the include will be picked up. you would then have something like ` – Kebab Programmer Aug 13 '20 at 10:54
  • It did look like the php statement was commented out. But, after renaming the file which contains the php statement to a .php, another problem occured. Whenever I click the anchor tag to go to that page (menu.php) it doesn't go to that page but tries installing it. – Ori Silberman Aug 13 '20 at 10:55
  • You can include documents inside other documents without using PHP using server side includes. Rename .html files to .shtml files, then you can include files like this: ``. Note that this only works in Apache, not nginx. – JNic Aug 13 '20 at 12:49

2 Answers2

0

From what you answer and ask on the reply’s under your question I wonder if you are aware Php code only works when run on a server? You can create your own server environment locally using Xampp.

HarmJan
  • 16
  • 1
-1

To use it the way you have you would have to rename the file to header.php and open and close the <?php ?> tags.

If you have no other need for php then I would do it with JQuery, see this answer

If you do have another need for PHP then I would suggest splitting your code into MVC or at lest seperating your backend code from your frontend code using a tmplateing engine like Twig for example.

Chris
  • 732
  • 2
  • 8
  • 19