Forum Moderators: coopster
Objective:
Fairly simple. I have a template, css/xhtml. I need to 'include' the content, possibly the menu, and definitely the Title tag (= 3 variables).
Problems (as I see them):
I only know how to include things this way:
<?php @ require_once ("$page.html");?>So therefore I will need up to three variables for each page, and a (for example) menu.html, title.html, and content.html as includes for each page. Is this right, or is there a more efficient way to do this? I am using mod_rewrite to clean up the urls. Having three variables also means that I have to represent them all in the 'clean url'. Seems kinda... 'cumbersomely long', no?
Hmm.. that's about it...
If the menu stays the same on all pages, use a separate include for it. Then, put each page of content AND it's corresponding title in a separate file, named by the variable you will use to call the content.
That only makes one variable to pass in the url, which you can do with a regular querystring because the major engines have no problem with one or two vars in the url(eg. .com?page=1234). Or, you can still use mod_rewrite if you wish.
Name your content files with a .php extension and in the file do something like this:
<?php
function title()
{
?>
>>> Your <title> and other <head> info here <<<
<?php
}function body()
{
?>
>>> Your main content here <<<
<?php
}
?>
Then, in your template:
<?php
include("1234.php");
include("menu.php");
?>
<html>
<head>
<?php
title();
?>
</head>
<body>
<?php
body();
?>
Something like that ;) I like to use a database to store my page information because the files don't keep piling up and I can develop easy searches for info via the db.
Birdman
One question though:
In your example above, in the second box, the one that represents my 'template', you put...
<?php
include("1234.php");
include("menu.php");
?>
I take it that the '1234.php' should actually be the variable that is given in '.com?page=1234'?
What is mixing me up here is that it appears to me, in your example, I am lacking a variable. No?
It would look like this:
include("{$page}.php");
This is assuming that the template AND the content files are in root. If not, simply adjust the include.
include("/pages/{$page}.php");
So, if you call your template with a url like this:
www.site.com/template.php?page=1234
1234.php will be included..
Hope that helps some.
Hope that helps some.
There's an understatement. It helps tonnes, though we are set up using 'require once', I'll give the includes a shot.
This all grew from a css-content-management article over at alistapart. It started simple, but, well, sans-database I suppose is still quite simple, just bigger!
Might I suggest for your example to use require rather than require_once. require_once must keep track of the files it has already included to ensure no double inclusion. The difference is most likely not noticable but require and include do slightly less work then their _once counterparts.
daisho.
worked like a charm. You have no idea how ecstatic I am right now. We have tonnes of content coming tomorrow and a Monday deadline. This was the last hitch.
Fwiw, I adjusted things so that they open and closed 'properly' in this part:
<?php
function title()
{
?>
Your <title> and other <head> info here
<?php
}
?><?php
function body()
{
?>
Your main content here
<?php
}
?>
Not sure if that is important at all, just did it that way though...Thanks again!
It depends on how you are set up. The variable would only be necessary if your script needs it to do its thing. There is no requirment for a variable. eg
<HTML>
<HEAD>
<TITLE XXXX </TITLE>
</HEAD>
<BODY>
<?
include("header.php");
include("menu.php");
?>
content
<?include("footer.php");
</BODY>
</HTML>
Should work fine for static pages.
If content is pulled from a db then it would become a variable that would need to be passed in your anchor.
I think that makes sense.
WBF
default.php:
<?php require( 'myLib.php' );?>
myLib.php:
<?php
//do some stuff that defines content and page title...
$pageTitle = 'xyz';
if( I-decide-something )
$header = 'a-header';
else
$header = 'a-different-header';
if( I-decide-something-else )
$body = 'a-body';
else
$body = 'a-different-body';
include( '$header. inc.php);
include( '$body. inc.php);
?>
a-header.inc.php:
<title><?php echo $pageTitle?>
asp
index.php
switch($page)
{
case "": // if empty
case "main":
include('the_main_file.txt');
break;
default:
include('404.php');
// I don't know if a break is good form/required/optional or what, here.
} Edit:
It's actually a lot more complicated now, but that's basically what it looks like.
You can also do neat pre/re-processing things with the output buffer functions ob_start, etc...
One last comment/question, a bit off of the original topic..
We are using mod_rewrite to fix up our Urls, which will come out looking like normal .html files. We have taken a couple other steps in our .htaccess file in order to keep users in the main directory etc...
[edited by: mipapage at 11:23 am (utc) on May 15, 2003]
Nick
wrt to the above question, I am going to start a new thread [webmasterworld.com] as the topic is just too different....
[edited by: jatar_k at 4:19 pm (utc) on May 15, 2003]
[edit reason] fixed link [/edit]