David
David

Reputation: 734

When including your header, in PHP, how do you add things?

I am building one of my first PHP websites, and just playing around with it. I am going to be using PHP includes, so I only have to change my meta stuff once. Currently, I have everything from the doctype to the end of the </head>.

A friend is saying I should not include the </head> on the header.php, but instead, include it on each page, so I can add page specific stuff. Is that a good way of doing it?

Currently, for stuff like title, I was doing

<title><?php echo $page_title; ?>

and then on the top of each page, I was doing

<?php $page_title = 'Blah'; ?> 

How would I add a page specific Javascript file if this is the way I'm doing this?

Upvotes: 4

Views: 874

Answers (4)

insertusernamehere
insertusernamehere

Reputation: 23590

I don't know, how you build up your pages. But on a simple site, you could use an associative array like this:

$pages = array(
    'home' => array(
        'title' => 'Home',
        'metaDescription' => 'This is the home of my website',
        'metaKeywords' => 'A,B,C',
        'scripts' => '<script src="src/js/com.jquery/1.7/jquery-1.7.min.js"></script>',
        'content' => 'home.phtml'
    ),
    'page_1' => …
);

And than you can recall it like this:

<head>
    […]
    <title><?php print $pages['home']['title']; ?></title>
    […]
    <?php print $pages['home']['scripts']; ?>
    […]
</head>
<body>
    <?php require '/pathToIncludes/' . $pages['home']['content]; ?> 
</body>

But I would only recommend this for a site with just a bunch of pages.

Upvotes: 2

Jimmy Sawczuk
Jimmy Sawczuk

Reputation: 13614

What's going on here is you're trying to inject logic into your templates, which isn't necessarily wrong, but it produces more confusing and harder-to-maintain code. This issue isn't only with your <title> tags, but will keep coming up as your pages get more and more dynamic and complex (dynamic navigation, page-specific layouts, etc.).

An MVC approach solves this problem perfectly. MVC consists of models, which talk to your data sources (MySQL, Redis, whatever) and handle your logic; views, which render HTML; and controllers, which are sort of the glue between models and views. Each request a user makes is eventually routed to a controller and then an action method in your controller (for example: /users/login might map to the User controller and the login action).

You would set your page title (or any other meta information) in the action method in your controller, which knows what the request is but is invoked before the view is rendered:

$request->setTitle('Home page');

And then in your view, simply render it:

<title><?php echo $request->getTitle(); ?></title>

If you're just starting PHP, it's a great time to learn MVC because it'll get you into some good habits that affect not only your PHP development but any other development as well.

I recommend you check out CodeIgniter for its simplicity and excellent documentation. I used CodeIgniter for a while, but ended up writing my own framework that fitted my needs better.

Upvotes: 6

Dmitry Teplyakov
Dmitry Teplyakov

Reputation: 2908

I think you should do some logic first, then render it. Something like this:

<?php

$page = array(
    'title' => 'Title',
    'js' => array(
        'jquery.min.js',
    ),
    'copyright' => 'Copyright 2012',
);

if ( $needed ) {
    $page[ 'js' ][] = 'some-other.js';
}

include( 'header.php' );
include( 'content.php' );
include( 'footer.php' );


// header.php
<html>
    <head>
        <title><?= $page[ 'title' ]; ?></title>

        <? foreach ( $page[ 'js' ] as $js ) { ?>
            <script src="<?= $js"></script>
        <? } ?>

    </head>

Upvotes: 2

Jordan Eldredge
Jordan Eldredge

Reputation: 1717

Here is how I solve this problem:

  • start.php connects to my database, includes my classes, sets all my default meta values into a $page object and starts $_SESSION
  • header.php Spits out all my html top matter (doctype, <head> etc.) based on the values in $page
  • top.php Includes start.php and header.php

For most files, the default values are fine. I just include top.php. For special cases where I need special values, my page looks like this:

<?php 
    include("start.php");
    $page->title = "My special title";
    include("header.php");
    // The rest of my content
?>

This has the added advantage that pages can have access to $_SESSION and other custom classes, and use that information to modify the header without getting the "header already sent" error. For example, you can validate some user input and redirect them using location('header: foo.php'); or change the title depending on their login status.

For page specific javascript includes, I would create an array of urls to include $page->javascript and then put something like: $page->javascript[] = "http://example.com/js/myScript.js"; between start.php and header.php

Upvotes: 1

Related Questions