Fr0z3n
Fr0z3n

Reputation: 1576

Dynamic page title

I have a index.php page that is the main page. All pages are included dynamically inside index.php by GET variables.

This is part of the uFlex class.. This will generate the title based on the filename of the file included and i don't like this way..

<?php

$page = @$_GET['page'];
$page = !$page ? "home" : $page;
$ext = ".php";
$page_inc = "page/" . str_replace("-", "_", $page) . $ext;
//Page not found
if(!file_exists($page_inc)) send404();
if(!$page_title){
$page_title = ucfirst($page);
}
?>

//   HTML CODE <head>, <title>, ecc
//   And then

<?php include($page_inc); ?>

Example: i view http://sitename.ext/?page=user&id=nickname

It will include user.php and since includes are made after the <title> tag.. i cannot set a title based on a php variable. Inside user.php i have $user['username'] and i want to set this var as a title..

I need to rewrite a new way to include files with the possibilty to set titles from the file included.

Suggestion? Thank you!

Upvotes: 1

Views: 1117

Answers (1)

You should consider using require, then call functions to generate data.

<?php require($page_inc); ?>
<html>
<title><?php echo $page_title; ?></title>
....
<div><?php echo $page_content?></div>

This is pretty much akin to what wordpress does, albeit a really high level.

Upvotes: 1

Related Questions