Print Reseller Scheme
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Curiosity - moving meta data to external file

Discussion in 'Website Design Forum:' started by Levi, Aug 28, 2009.

  1. Levi

    Levi Moderator Staff Member

    Ok I've been having one of my random days where I think about things from a tangent (yeah I know :cry:)

    Anyways as I've been playing with my site again I had this thought about all the stuff at the top regarding meta tags/keywords etc. As its all the same apart from the title on each page can you stick it in an external file which is accessed via a link in the same way as a php file or script file would be?

    It just seems like a logical idea to do this so that things like the description or keywords can easily be changed on all the pages without having to do all of them - before you say it, I'm not using a template as I haven't got any identical pages so far :(.
  2. Kevin

    Kevin Senior Member

    Well... how often are you planning on changing them anyway? :confused:

    You could try replacing all the meta stuff by
       <?php include("meta.html"); ?>
    Although I'm not sure how this would work out. And it seems to me that it might slow down your page and/or affect your search engine friendliness (how should I say that? :p).

    Oh and I forgot to add...
  3. Levi

    Levi Moderator Staff Member

    I don't intend to change them that often but my curiosity got the better of me :)
  4. Harry

    Harry Senior Member

    Simple. You need a header.php file and in it you have:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "">
    <html xmlns="" xml:lang="en" lang="en">
        <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
        <meta name="description" content="DESCRIPTION" />
        <meta name="keywords" content="KEYWORDS" />
        <meta name="author" content="Harry Roberts" />
        <title><?php echo $pageName?></title>

        <link rel="shortcut icon" href="/favicon.ico" />
        <link rel="stylesheet" type="text/css" href="/css/style.css" media="screen" />
        <link rel="stylesheet" type="text/css" href="/css/print.css" media="print" />
    <body id="<?php echo $pageID?>">

    and then all you need to do in your pages is this:

    'My Site homepage';
    $pageID 'home';
    [content in here]
    All your meta information is contained in header.php and is only ever written once. The two PHP variables change on a page by page basis. I use a similar but slightly more advanced solution on my site.
  5. Levi

    Levi Moderator Staff Member

    cool, so it can be done, I was along the right lines when I was playing last night atleast, I just didn't take enough of the header text and get the reference bit right on the page :)

    Not sure if I will use it but its nice to know that my curiosity wasn't trying to kill me :)
  6. Harry

    Harry Senior Member

    Not only can it be done, it's how it should be done ;)

    Take it a step further and use a config file:
    and then parse that: PHP: parse_ini_file - Manual
    then you have all your site data contained in a config file rather than in the markup.
    Then your header.php wouldn't contain the actual keywords, it'd contain a keywords constant that pulls data from the config.ini file.

    Easy peasy :D
  7. Kevin

    Kevin Senior Member

    Err... I'll just try the first method you showed... The last one kind of scares me :p
  8. Harry

    Harry Senior Member

    Haha, takes some getting used to if (like me) you don't work with prog. You should also have a footer php include too by the way, which will contain the closing body and html tags as well as any Google tracker code. And of course a nav.php include is a very sensible idea.
  9. Kevin

    Kevin Senior Member

    Soo... what effects (positive or negative) do all these includes have on your site? Other than improved maintainability?
  10. Harry

    Harry Senior Member

    They are just maintainability based. Have you ever tried updating the navigation menu on a site when the menu is written once per page? Includes will save your life haha. Also I guess it saves space on the server, but not a great deal. I didn't used to use includes but I wouldn't stop now even if someone paid me!

    EDIT: Just spoke to a mate at work about this and he also says they help with code reuse. You can copy and paste a header include from one site to another and just update the necessary bits and you're away. All the code is prewritten, you just have to update names/meta content etc.

  11. Kevin

    Kevin Senior Member

    Well so far the only sites I've ever made were a couple pages big. So adjusting the nav isn't much of a hassle then. But I'll think I'll use includes from now on though :up:
  12. Levi

    Levi Moderator Staff Member

    yeah I think I'll be sticking with the easier option too then :)
    Its not like I'll be changing the layout on my site too often anyways but its always nice to learn something new or a better way of doing things.

    So am I right in thinking that by using the 'php include' part I can link to any php file which is using say some multiple use html in it, if I am that might come in handy on my gallery page rather than copy and pasting a load of stuff several times (which will be a pain if I ever change stuff) :)
  13. Harry

    Harry Senior Member

    Does that mean you will or won't be using includes?

    Includes are such good practice. It means you can make changes so quickly, and expand the site (should you need to)

    Exactly, if you want to reuse code just pop it somewhere (in an includes directory) and just include it by pointing the php include() function at it.

    Here are the includes I use on my site:

    &mdash; SITE_NAME &mdash; CSS, Web Standards, Typography, Grids and now HTML5
  14. Levi

    Levi Moderator Staff Member

    I'm going to try and implement the includes on my menu, meta and in my gallery when I get there, hopefully that should all work but we'll see if I can get it to work or not :).
    Now I've just got to find the time to actually have a go, I don't think trying at 12am is the best time to try this :)
  15. Harry

    Harry Senior Member

    It probably is the best time. I work way better with tricky stuff at night!

    They're so easy though. You literally just build a page as a whole then cut bits out and paste them into include files.
  16. Levi

    Levi Moderator Staff Member

    ok had my first go at it and it doesn't like me :(

    I literally took your code Harry (adjusted it to suit mine) and I don't get any title anymore, metatags aren't being registered on the website grader site (they were before). Also got some issues with validation now too :(

    link to original page
    link to adjusted page

    header code (saved as includes/header.php - added /test into the link to test though as its in a folder)
    <link title="favicon" rel="shortcut icon" href="/media/favicon.ico"/>
    <link title="Apple Mobile OS-X Bookmark" rel="apple-touch-icon" href="/media/apple-touch-icon.png" />
    <meta name="description" content="Image Resolutions etc" />
    <meta name="keywords" content="3D Design, Modelling Rendering" />
    <meta name="author" content="Image Resolutions" />
    <meta name="owner" content="Image Resolutions" />
    <meta name="designer" content="Image Resolutions" />
    <meta name="copyright" content="Image Resolutions 2004-2009" />
    <title><?php echo $pageName?></title>
    <body id="<?php echo $pageID?>">
    I've tried it with the first couple of lines in it too but that kept asking for my encoding everytime I opened it.

    Is it me and I've managed to design a site that doesn't work with any helpful tricks or is it that I've just missed something off?
  17. Harry

    Harry Senior Member

    It had to be a .php fileā€¦ you're using .html ;)
  18. Levi

    Levi Moderator Staff Member

    ah, so in theory if I resave as a php it should fix it, I'll try it now
  19. Harry

    Harry Senior Member

  20. Levi

    Levi Moderator Staff Member

    nope seriously does not like me, makes the page completely blank :confused:

    I just went save as and set to php, thats right yeah?

    edit:hmm its fine when I preview it in firefox, just not from the server

Share This Page