PHP template driven site help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pauly
Forum Newbie
Posts: 4
Joined: Wed May 29, 2002 8:38 am

PHP template driven site help

Post by pauly »

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.
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

Simply, it can be something like this:

Code: Select all

<?php

include_once 'header.php';

?>

// content of site goes here

<?php

include_once 'footer.php';

?>
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.
User avatar
gotDNS
Forum Contributor
Posts: 217
Joined: Tue May 07, 2002 5:53 pm
Location: West Chester, PA

Post by gotDNS »

PHP.net is always a good place to start :P

http://www.php.net/manual/en/function.include.php
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

i make one file, called layout.php and it looks something like this:

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>
and then for each page i write a code like this:

Code: Select all

<?
$content = <<<HTML
 
 - some content i want using html goes here -

HTML;
include('layout.php');
?>
sorry if you dont understand, but i find it the easiest way
User avatar
riley
Forum Commoner
Posts: 45
Joined: Thu May 02, 2002 6:31 pm

Post by riley »

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>
User avatar
Grunge
Forum Newbie
Posts: 4
Joined: Wed Jun 05, 2002 7:36 pm
Location: California
Contact:

Post by Grunge »

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.
-----------

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>
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.
-----------

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";
?>
Here's pagecontent.txt - Put what you need! :)
-----------

Code: Select all

JAKj aslkjfd laskdfj lkjsd fehjf fweee!
<br>
<a href="http://www.here.com">BOO!</a>
Here's extracontent.txt - Put what you need! :)
-----------

Code: Select all

this is in bold! :)
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.
saleem
Forum Newbie
Posts: 1
Joined: Sun Oct 20, 2002 2:33 pm
Location: Manchester
Contact:

Post by saleem »

I know this post was done absolutely ages ago, but i must say i was having trouble with this, and the explanation by Grunge really helped me a lot.

Thanks,

Saleem
User avatar
Heavy
Forum Contributor
Posts: 478
Joined: Sun Sep 22, 2002 7:36 am
Location: Viksjöfors, Hälsingland, Sweden
Contact:

Post by Heavy »

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.
[]InTeR[]
Forum Regular
Posts: 416
Joined: Thu Apr 24, 2003 6:51 am
Location: The Netherlands

Post by []InTeR[] »

If you want some simple template functions try
Yet Another PHP Template Engine
Mr. Tech
Forum Contributor
Posts: 205
Joined: Tue Feb 11, 2003 4:18 pm
Location: Australia

Post by Mr. Tech »

I just have a header and a footer file:

Code: Select all

<?php include("head.php"); ?>

Content goes here.

<?php include("foot.php"); ?>
:D
Post Reply