Page 1 of 1

Deferred dynamic <title> replacement

Posted: Sat Nov 13, 2010 4:21 am
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.

Re: Deferred dynamic <title> replacement

Posted: Sat Nov 13, 2010 12:39 pm
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>