0
votes

I am trying to move the doctype, head, scripts etc, and opening body tags to a header.php include file. I am setting up php variables for page title etc.

All pages use the same scripts except one which needs an extra script.

I can't work out how to do this?

How can I add an extra tag to a page which uses a header template?

Thanks for any help!

2
Instead of including that extra script in the header conditionally, why not just include it on the one page where you need it? - APAD1
The header.php extends to the opening body tag. If I just include it on that page it won't be in the head section? - Lee Colarelli
Why does it need to be in the head? - APAD1
Because of code OCD :) - Lee Colarelli
Well, OCD aside, you shouldn't be loading scripts in the head anyway. It's best to load scripts as close to the bottom of the page as possible(typically right before the closing body tag). The exception to this would be important scripts, such as jQuery. - APAD1

2 Answers

1
votes

Simplest solution:

the script that needs the extra bit:

$show_extra = TRUE;
include('header.php');

header.php

<html>
<head>
if (isset($show_extra) && ($show_extra)) {
   ... output extra bits
}
</head>
etc...
1
votes

You can have a header($array_scripts) function, that display your header and all the scripts inside.

Let's say your scripts are CSS files, and your files are organized that way :

/www
---- /css
     ---- script1.css
     ---- script2.css
---- index.php
---- header.php

On your page index.php :

include_once("header.php");
$scripts = array("script1.css", "script2.css");
header($scripts);
// Rest of the code : <body>

And in your header.php file :

 function header($array_scripts)
{
  // display everything, like <!doctype HTML>, <head> tag...
  foreach($array_scripts as $script)
  {
     echo '<script src="css/'.$script.'">';
  }
  // close tags like </head>
}

You can even give your title, description, ... as parameters to your header function. For example, if your header function has this prototype :

header($title, $array_scripts);

You will be able to display the title in header.php with:

// <head> ...
echo '<title>' . $title . '</title>';