0

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!

6
  • 1
    Instead of including that extra script in the header conditionally, why not just include it on the one page where you need it? Commented Dec 2, 2014 at 16:45
  • 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? Commented Dec 2, 2014 at 16:50
  • Why does it need to be in the head? Commented Dec 2, 2014 at 16:59
  • Because of code OCD :) Commented Dec 2, 2014 at 17:14
  • 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. Commented Dec 2, 2014 at 17:18

2 Answers 2

1

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...
Sign up to request clarification or add additional context in comments.

3 Comments

Can't gets to work - <?php if (isset($gmap_api) && ($gmap_api)) { <script src="maps.googleapis.com/maps/api/js"></script> } ?> What parentheses around script tag?
you've got html directly in your php. that's a syntax error. you need to echo out the script, or <? if(...) { ?><script>...</script><?php } ?>
Thanks, I have fixed the code now with your help. It's all working :)
1

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>';

2 Comments

I like this answer but it's a bit too complicated for me :) Can't quite get my head round it.
I've edited the answer, i hope it's a bit clearer now... Let me know if you have trouble understanding ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.