Deferred dynamic <title> replacement

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
lorenzo-s
Forum Commoner
Posts: 43
Joined: Tue Aug 25, 2009 12:25 pm

Deferred dynamic <title> replacement

Post by lorenzo-s »

Hi. I'm working on a PHP website that generate HTML output in this way (I reduce to the minimum, please don't say I need $_GET validation!):

Code: Select all

Example URL:      /index.php?page=user&uid=657
index.php:        <head><title>My Site</title></head><body><?php include(...) ?></body>
pages/user.php:   <p><?php /* User data */ ?></p>
I cannot use a template system or a different way to generate pages (I'm working in team :cry: ).

I need to change the <title> tag depending on content of inner page. In the example above, I need to print the user name. Ok, so, how can I do it?

:roll: A. Increase coupling. I use a case stament in the index.php, and if page parameter is user, I read the user and print the username in the title. That requires an editing each time I want to manage new pages title.
:crazy: B. Increase insanity. The website is currently using output buffer. I can get it into the inner page with ob_get_clean(), use a simple regular expression to replace the title, and then re-echo.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Deferred dynamic <title> replacement

Post by requinix »

C. Include the file at the beginning of index.php (with output buffering) and have the file define a variable or constant with the page title.

Code: Select all

<?php

ob_start();
include "whatever.php"; // define("TITLE", "My Site");
$html = ob_get_clean();

// <title><?php echo TITLE; ?></title>
Post Reply