Israel
Israel

Reputation: 1224

HTML tags question

Something basic that i don't understand:

I have header.php with navigation bar for my site. Inside it, there's a <head>...</head> section.

Now, in each other page of my site, I'm using require_once 'header.php' so that each page will show the navigation bar. But, I need also specific <head>...</head> sections to the different page.

For example, in page customers.php, I'm using <script>...</script> to include the jQuery library. I don't need to include it in other pages.

Now, searching the web I see that multiple head tags is wrong syntax.

So, how can anyone:

  1. avoid multiple "head" tags
    WHILE
  2. separating his work to different PHP files and including them ?

Upvotes: 1

Views: 136

Answers (4)

Michas
Michas

Reputation: 9468

One possible solution.

  1. You create a global variable before including header.php.
  2. You test this variable in header.php.
  3. If it is true, You print script or something. Something like this:

    <!-- Fragment of header.php -->
    <?php if ($i_want_jquery): ?>
    <script ...>
    ...
    </script>
    <?php endif; ?>
    

On the other hand, a template may be a better solution.

Upvotes: 0

Your Common Sense
Your Common Sense

Reputation: 157991

You have to change your page structure and employ templates.
Instead of loading header at the top of the code, you have to do it at the bottom! And page code should output not a word, but collect all data in variables. And only after that output can be started by calling template.

A example layout is going to be like this:

First. page itself.
it outputs nothing but only gather required data and calls a template:

<?
//include our settings, connect to database etc.
include dirname($_SERVER['DOCUMENT_ROOT']).'/cfg/settings.php';
//getting required data
$DATA=dbgetarr("SELECT * FROM links");
$pagetitle = "Links to friend sites";
//etc
//and then call a template:
$tpl = "links.php";
include "template.php";
?>

Next, template.php which is your main site template, consists of your header and footer:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>My site. <?=$pagetitle?></title>
</head>
<body>
<div id="page">
<? include $tpl ?>
</div>
</body>
</html>

And, finally, links.php is the actual page template:

<h2><?=$pagetitle?></h2>
<ul>
<? foreach($DATA as $row): ?>
<li><a href="<?=$row['link']?>" target="_blank"><?=$row['name']?></a></li>
<? endforeach ?>
<ul>

easy, clean and maintainable.

there are many advantages in such approach:

  • as requested, you can populate header with actual page-relevant data.
  • HTTP headers can be sent as well, before any output. It includes cookies, sessions, cache-control and many more.
  • it's 2011 today. AJAX era. You may wish change your code to return JSONed data instead of whole HTML page. It's going to be easy using such layout.
  • Imagine you're going to create very similar site with just different design. You will have to change only templates and don't touch engine files. That's really great advantage of using templates.

Upvotes: 1

Chien-Wei Huang
Chien-Wei Huang

Reputation: 1861

in header.php you can type like this

<head>    
<?php echo $script; ?>
</head>

then in your customers.php you can first assign the variable

$script = '<script>...</script>'

then

require_once 'header.php'

Upvotes: 0

Andre Backlund
Andre Backlund

Reputation: 6953

Here are some simple ways you can look at.

  • You can have jQuery on the pages that don't need it; once it's downloaded it will be cached so it still wont use more bandwidth.

  • You can move out the closing </head> tag from header.php and close the <head> tag in the page that's including header.php.

  • You can include javascript anywhere on a page, not only in the header.

You can also do something like this. Before you do require_once 'header.php'; you put a variable called $jquery = true;

In your header.php file you check if $jquery is set to true, if it is, you include jQuery.

Upvotes: 0

Related Questions