简体   繁体   中英

Add extra scripts to a page with header.php include file?

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!

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...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM