Page 1 of 1

PHP template driven site help

Posted: Wed May 29, 2002 8:38 am
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.

Posted: Wed May 29, 2002 9:26 am
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.

Posted: Wed May 29, 2002 1:49 pm
by gotDNS
PHP.net is always a good place to start :P

http://www.php.net/manual/en/function.include.php

Posted: Wed May 29, 2002 2:26 pm
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

Posted: Thu May 30, 2002 6:29 am
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>

Posted: Wed Jun 05, 2002 7:36 pm
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.

Posted: Sun Oct 20, 2002 2:33 pm
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

Posted: Mon Apr 28, 2003 3:32 am
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.

Posted: Mon Apr 28, 2003 3:38 am
by []InTeR[]
If you want some simple template functions try
Yet Another PHP Template Engine

Posted: Mon Apr 28, 2003 3:52 am
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