Stefan S.
Stefan S.

Reputation: 4103

Customizing HTML Pages Using PHP

I'm having a bunch of HTML pages that are almost equal except for some minor details, like including a second CSS file, a unique title and a custom favicon. I don't like the idea of maintaining all these pages individually. A naive way to achieve what I want is to use some GET or POST parameter to differenciate the details like that:

$customization = array( "favicon" => "favicon_dummy.ico", "title" => "My Page");
if ($_GET["id"] == "my-first-page") 
    $customization["favicon"] = "favicon_first.ico"
if ($_GET["id"] == "my-second-page") 
    $customization["title"] = "My Second Page"

And then something like that:

<head>
<title><?php echo  $customization["title"]; ?></title>
<link rel="shortcut icon" href="<?php echo  $customization["favicon"]; ?>" type="image/x-icon" />
</head>

Now that is a pretty crude way, and I think it's error-prone. What is the correct way (TM) to achieve such tasks?

Upvotes: 0

Views: 83

Answers (2)

Luke
Luke

Reputation: 4063

I wouldn't say there's any correct way to do this since there is multiple ways that PHP can retrieve and manipulate data then render it to the browser. What I would say is consider the future of your website/application and whether this solution will work for very long.

If you're simply requiring static information then I would have an assets.php page, filled with either variables or definitions of the various things you need. i.e.:

<?php

switch( $_SERVER["PHP_SELF"] )
{
    case "/index.php":
        $_css_url   = "path/to/style.css";
        $_js_url    = "path/to/javascript.js";
        break;

    case "/about.php":
        $_css_url   = "path/to/about/page/style.css";
        $_js_url    = "path/to/about/page/javascript.js";
        break;    
}

$_copyright = "Copyright &copy; mysite.com";

?>

You can of course use an array (as you alluded to) but that just means you're writing the array name AND the variable name every time.

Then on your main pages, require/include the asset file and use the variables appropriately -

<?php

require_once("assets.php");

?>
<!DOCTYPE html>
<html>
   ...
   <?php echo $_copyright ?>
   OR
   <?= $_copyright ?>
   ...
</html>

Or use definitions in your assets.php:

PHP define() docs

<?php

define( "CSS_PATH", "path/to/style.css" );
define( "JS_PATH",  "path/to/javascript.js" );

define( "MY_COPYRIGHT", "Copyright &copy; mysite.com" );

?>

and

<?php

require_once("assets.php");

?>
<link rel="stylesheet" type="text/css" href="<?= CSS_PATH ?>">

Upvotes: 0

Danijel
Danijel

Reputation: 12689

I guess the use multidimensional array with values for all pages can be solution, and then you could extract and use the values for current page:

<?php

$customization = array( 
    'default' => array( "favicon" => "favicon_dummy.ico", "title" => "My Page" ),
    'my-first-page' => array( "favicon" => "favicon_first.ico", "title" => "My First Page" ),
    'my-second-page' => array( "favicon" => "favicon_second.ico", "title" =>  "My Second Page" )
);

if ( isset( $_GET["id"] ) && array_key_exists( $_GET["id"], $customization ) ) {
    extract( $customization[ $_GET["id"] ] );
} else {
    extract( $customization[ 'default' ] );
}

?>
<!DOCTYPE html>
<html>
<head>
    <title><?php echo $title; ?></title>
    <link rel="shortcut icon" href="<?php echo $favicon; ?>" type="image/x-icon" />
</head>

But i advise you not to do that. It can be only suitable for a small number of pages and customizations. For everithing larger that that much better solution is to use database and to store there everything related to certain page. Since you don't mention the databases i'm gonna guess that you don't use them. Database table for your needs can be structured with columns like:

page_id | page_slug | page_title | page_content | page_favicon | page_css | ...

Databases are also much more than just a place to store data, and if not used, all that capabilities will have to be coded by hand. Is definitely worth to invest some time in them.

Upvotes: 2

Related Questions