0

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?

2
  • Usually if most html pages are the same and you don't want to repeat html code then you use (include or require). --- w3schools.com/php/php_includes.asp Commented Jul 6, 2014 at 20:29
  • @Tasos I don't think that's the best approach for this specific problem, since that would chop up code that belongs together into different files and make it a lot less maintainable. And just for adding a custom icon... I don't know. Commented Jul 7, 2014 at 10:14

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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 ?>">

6 Comments

Does this mean I have to use a switch block inside the the assets.php to differenciate? Or before loading different files?
No, why would you have to do that? You're just giving variables values then referencing them in another file
How does that customize the HTML? If there is only one set of variables I'll get the same resulting file each time.
That's what you asked for? A way to include information that is repeated across several pages
I meant it the other way around. The data of the assets.php is what is different in each of my files, the bigger part (i.e. the actual HTML file) is what is the same for each page.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.