Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

javascript

php

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?

like image 869
David Avatar asked Dec 20 '22 19:12

David


2 Answers

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.

like image 186
Jimmy Sawczuk Avatar answered Jan 09 '23 01:01

Jimmy Sawczuk


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>
like image 42
Dmitry Teplyakov Avatar answered Jan 09 '23 00:01

Dmitry Teplyakov