PHP template driven site help
Moderator: General Moderators
PHP template driven site help
Hi,
My problem is I currently have over 300 pages on my website which I have to change individually which takes months. I have heard that with php includes you can create a template file and then the other html pages are loaded into the template.
I am a bit confused by it all but have been searching the web for a month now looking for a newbie tutorial on this. I can't find anything, I was really hoping someone could reply to this post and point me in the right direction.
I would really, really appreciate the help.
Thanks.
My problem is I currently have over 300 pages on my website which I have to change individually which takes months. I have heard that with php includes you can create a template file and then the other html pages are loaded into the template.
I am a bit confused by it all but have been searching the web for a month now looking for a newbie tutorial on this. I can't find anything, I was really hoping someone could reply to this post and point me in the right direction.
I would really, really appreciate the help.
Thanks.
Simply, it can be something like this:
And you would simply create a header and footer file that contains the HTML code you want to use as the header and footer. This would mean if you wanted to change the layout, you could easily change simply the header and footer layouts.
Code: Select all
<?php
include_once 'header.php';
?>
// content of site goes here
<?php
include_once 'footer.php';
?>- hob_goblin
- Forum Regular
- Posts: 978
- Joined: Sun Apr 28, 2002 9:53 pm
- Contact:
i make one file, called layout.php and it looks something like this:
and then for each page i write a code like this:
sorry if you dont understand, but i find it the easiest way
Code: Select all
<html>
<head>
<title> My Layout</title>
</head>
<body>
<table>
<tr>
<td>Content</td>
</tr>
<tr>
<td>
<?
//THIS IS WHERE THE CONTENT WILL LOAD
echo "$content";
?>
</td>
</tr>
</table>
</body>
</html>Code: Select all
<?
$content = <<<HTML
- some content i want using html goes here -
HTML;
include('layout.php');
?>This will send the typical page (heading at the top)(Menu on the left)(Body on the right). Then just pass the the page (as ?page) containing just the necessary html code to this starting page. You can also have pass different title heads and menus (multiple also, just include multiple pages at the menu line).
<HTML>
<HEAD>
<TITLE></TITLE>
<?
IF(!$page)$page = 'home.php';
include("include/css.php");
?>
</HEAD>
<Body BgColor='#FFFFFF'>
<TABLE Width=600>
<TR>
<TD Colspan=2>
<?
include("tbhead.php");\\ top title heading
?>
</TD>
</TR>
<TR>
<TD Width='150' VAlign='Top' Align='Left'>
<?
menutable.php");\\left menu items
?>
</TD>
<TD Valign='Top' Width='450'>
<?
include("$page");\\body page
?>
</TD>
</TR>
</TABLE>
</Body>
</HTML>
<HTML>
<HEAD>
<TITLE></TITLE>
<?
IF(!$page)$page = 'home.php';
include("include/css.php");
?>
</HEAD>
<Body BgColor='#FFFFFF'>
<TABLE Width=600>
<TR>
<TD Colspan=2>
<?
include("tbhead.php");\\ top title heading
?>
</TD>
</TR>
<TR>
<TD Width='150' VAlign='Top' Align='Left'>
<?
menutable.php");\\left menu items
?>
</TD>
<TD Valign='Top' Width='450'>
<?
include("$page");\\body page
?>
</TD>
</TR>
</TABLE>
</Body>
</HTML>
This is how someone tought me templates. It has kind of a lot of files but it's really easy and more 'template like' then includes.
This is the actual html goodness that is the template - I called mine pup.php - I simplified all the coding whohaw since I had a lot of image map crap and javascript that got in the way.
-----------
This is the actual page you'll call up in you browser (http://www.yersite.com/myfirstpage.php - it's going to mix together all the content goodness with the template above.
-----------
Here's pagecontent.txt - Put what you need! 
-----------
Here's extracontent.txt - Put what you need! 
-----------
The good part is, it's an actual template where you can alter anything you need without disturbing any of the files and are able to change all the appearences on your site.
The bad, buggy with other PHP scripts links into the template (but I have a feeling that's fixable) and this method produces a LOT of files that need to be addressed individually (ie: a page called art.php, if it has two external content files included in the template, would be called art2.txt, art3.txt ... see?)
But it works good for me. Good luck.
This is the actual html goodness that is the template - I called mine pup.php - I simplified all the coding whohaw since I had a lot of image map crap and javascript that got in the way.
-----------
Code: Select all
<html>
<head>
<title>$sitename</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<img src="mylogo.gif">
<br>
<a href="somelink.php">Somelink</a>
<a href="someotherlink.php">Someotherlink</a>
<a href="again.php">Again</a>
<br>
<br>
$content
<br>
<br>
<b>
$extra
</b>
</body>
</html>-----------
Code: Select all
<?php
$sitename = "This Is My Page!"; // There's the title.
$cdat = fopen("pagecontent.txt", "r");
$content = fread($cdat, filesize("pagecontent.txt")); // There's the simple text doc that holds all the content goodness
fclose($cdat);
$edat = fopen("extracontent.txt", "r");
$extra = fread($edat, filesize("extracontent.txt")); // There's the text file goodness that will appear in bold
fclose($edat);
$template = addslashes(implode("", file("pup.php"))); // See! There's the template file thingy :)
eval("\$template = "$template";");
$template = stripslashes($template);
echo "$template\n";
?>-----------
Code: Select all
JAKj aslkjfd laskdfj lkjsd fehjf fweee!
<br>
<a href="http://www.here.com">BOO!</a>-----------
Code: Select all
this is in bold! :)The bad, buggy with other PHP scripts links into the template (but I have a feeling that's fixable) and this method produces a LOT of files that need to be addressed individually (ie: a page called art.php, if it has two external content files included in the template, would be called art2.txt, art3.txt ... see?)
But it works good for me. Good luck.
- Heavy
- Forum Contributor
- Posts: 478
- Joined: Sun Sep 22, 2002 7:36 am
- Location: Viksjöfors, Hälsingland, Sweden
- Contact:
This topic is really sleeping, but I want to add:
Take a look at how phpBB is written:
http://www.phpbb.com/
Those templates are nice. I don't know if they chache them though or if templates are parsed at every single client request.
Take a look at how phpBB is written:
http://www.phpbb.com/
Those templates are nice. I don't know if they chache them though or if templates are parsed at every single client request.
If you want some simple template functions try
Yet Another PHP Template Engine
Yet Another PHP Template Engine
I just have a header and a footer file:

Code: Select all
<?php include("head.php"); ?>
Content goes here.
<?php include("foot.php"); ?>