Curiosity - moving meta data to external file

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 :(.
 
Well... how often are you planning on changing them anyway? :confused:

You could try replacing all the meta stuff by
PHP:
   <?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...
curiosity-killed-the-cat.png
 
I don't intend to change them that often but my curiosity got the better of me :)
 
Simple. You need a header.php file and in it you have:

PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<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" />
	</head>
<body id="<?php echo $pageID; ?>">

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

PHP:
<?php
$pageName = 'My Site homepage';
$pageID = 'home';
include("/includes/header.php");
?>
[content in here]
</body>
</html>

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.
 
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 :)
 
Onartis said:
Err... I'll just try the first method you showed... The last one kind of scares me :p

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.
 
Soo... what effects (positive or negative) do all these includes have on your site? Other than improved maintainability?
 
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.

My Mate said:
They are handy for code reuse. I have a pagination includes that I have used for a few sites at sense now. Especially for things like pagination which are a pain in the arse to code. I suppose code reuse and keeping things neat and in a logical place are the two main reasons.
 
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:
 
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) :)
 
Levi said:
yeah I think I'll be sticking with the easier option too then :)

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

Levi said:
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.

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

Levi said:
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) :)

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
http://csswizardry.com/includes/nav.php
http://csswizardry.com/includes/subcontent.php
http://csswizardry.com/includes/footer.php
 
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 :)
 
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.
 
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)
PHP:
<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>
    </head>
<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?
 
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
 
Back
Top