homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

Php templates
I need to know how to make them...

 3:39 pm on Jul 14, 2004 (gmt 0)

I posted this a few weeks ago but the thread died, plus it was filled with clutter and a confusing name... I'll try this one as long as the moderator doesn't kill me >.<

I need to know how to create a php template and how to modify it to my site. Basically I need a tutorial that explains the in's and out's of templates. I had the code sent to me but I don't have an explanation... a breakdown of the code if u will. I'm new to php and only know the php require code. Anyone that can help me is GREATLY thanked!



 4:12 pm on Jul 14, 2004 (gmt 0)

Are you trying to build a template or use an existing templating system?

If trying to build a template, the main thing is to get all your non-outputting processing done before you get to the template and all output comes from the template.

Otherwise, the Smarty template system seems highly regarded.



 4:43 pm on Jul 14, 2004 (gmt 0)

No i'm just trying to make a template for a new layout. I just wanna have a "shell" so when I change my layouts I have to change ONE page and all the content is saved as .txt

I just wanna build a page that everything opens up in. Get it?


 5:30 pm on Jul 14, 2004 (gmt 0)

maybe try this thread
A dynamic site in 2 minutes [webmasterworld.com]


 8:54 pm on Jul 14, 2004 (gmt 0)

Lol Jatar u said that in my other thread lol, it doesn't answer what I need though, I already have a dynamically linked site, I just need to make a template and that has nothing on it. I need a walkthrough, a step by step tutorial ya know?

Ok hold on a second, i looked again and have an idea, but i have questions >.<


include 'includes/header.php';

$content = "pathtocontentpage.html";
include "template.php";

include 'includes/footer.php';


Ok can I make a page, like index.php and have all of the head (including the:


information inside, and still have browsers open it (because I know it has to be on top, but if it's dynamic, how will it know.

2. $content = "pathtocontentpage.html";
include "template.php";

What if the content is ever changing... wait... see... see that's confusing me... would every page need a different link to whatever page it is... like... if the page is... staff.php would i have to use content=staff.php and if index.php would i do staff.php or is it all in one big lump... Also can the files be saved at .txt

Then why is inclue "template.php"' there if your really just USING the template... so I would think...

It's dynamic... but not QUITE what i'm looking for, but still better then what I have I guess... I had something else but i'll hold it off for now since I forget, but is this one page... or every page created like this... Doesn't really tell u exactly what to do ya know?

Thanks for any help ^.^

[edited by: Echo_Tyme at 9:07 pm (utc) on July 14, 2004]


 9:05 pm on Jul 14, 2004 (gmt 0)

hehe, see what happens when you have multiple threads on the same topic. ;)

making a template is very simple but you can also make it as complex as you need/want

this would be a template


<head><title>welcome to example.com</title></head>
<?= $content?>

then in each file you could have


$content = 'somepage.html';
include 'template.php';

very straight forward.

or you could have a template that understands what part of the site it is being included in and behaves accordingly.

you could have one that gets sent a get string and pulls all the content from a db (with rewritten urls of course)

There are also many other ways to do it. You may not have gotten many responses because 'how do you make a template' isn't really a question that means anything. The thread I quoted (twice ;)) gives the gist of making template systems. The specifics come down to the individual designing it and their requirements.


 9:11 pm on Jul 14, 2004 (gmt 0)

posted right before you lol.

now ur examples, what if every page is different like with multiple pages... do u... actually PUT index.php IN The index.php file? I gotta find the code my friend gave me, it was confusing but supposedly it was one set page with all teh code, and then .txt files were called according to what u clicked... how that worked i'll never know... I dunno it's just... REALLY confusing to me... Don't think of me as a webmaster, think of me as a 4 yr old. That'll help explain lol. And I know u posted twice and the second time i looked it was very helpful lol, thank u lol.


 9:50 pm on Jul 14, 2004 (gmt 0)

I will explain my preferred method.

I have a header.php file that contains all the common html up to where the content of the page begins.

I also have a footer.php that contains all the common html after the page content ends.

So I use something like

<? include $_SERVER['DOCUMENT_ROOT'] . '/templates/header.php';?>

<p>This is the content of the page.

<? include $_SERVER['DOCUMENT_ROOT'] . '/templates/footer.php';?>

So every page on my site actually exists and has content in it.

That make any sense?


 11:06 pm on Jul 14, 2004 (gmt 0)

I actually already use that...

I have a page called up and it loads every other page.. I do this.

call index.php

this loads a menu, side.php, top.php and bottom.php while the content is the only constant of the page, that I know, that's just a dynamic page...

What I want is where you have one page, that's it... just one page that has all the html/php code... and then from that page, it calls up .txt files that are the actual content.

My friend sent me the code and it looks like this:

oh i don't have it... damn... it was a complicated small code, it was calling that if x=.html then perform this, and if it's not then call taht x=.php and it was the most confusing thing I have seen.

What your saying is a dynamic page, that's what I have now (except i didn't know i can do everything (from <html> down). What i'm looking for is just one page with the code, then all the content are saved as .txt and called up when a link is chosen.

Supposedly that's why you see things in the url like =1235-asfa.php and stuff like taht (so he says).

Thanks for what you wrote, but I have that, I use a php require and have all the dynamic pages load with me, but I need one page with code, then all the content is loaded whatever link is chosen...


 12:27 am on Jul 15, 2004 (gmt 0)


I sympathize as I couldn't find anything about how to make a template either. So I created my own. See message #59.


Basically each page just identifies the text files and calls the template. I use flat files for each page but it could just as well be a database.


 1:32 am on Jul 15, 2004 (gmt 0)

Hmm It gave me some good ideas I never knew php can do... after the <html> tag can I add whatever I want?... So can I have it print whatever I want... wherever I want? So if I want it to print a predetermined title... i can have it do so right? Can I have it called from a page? such as:

print (file extention and name) (what i want printed).


print /include/template.php $title

could I DO that? if not then can I php require a page after the html tag?

php require /include/template.php

then load what I please, where I please?

If so then this can have all the objects I have... in a pre-determined spot, meaning I can have all my tables loaded with a single line, and any change is changed ONCE, then I can end it and begin it, with a whole new case!

Now... is this possible and.. is it wise? If i want php requires ALL over the page with a little content... is that wise... or is that stupid? Aslo if i have one page to control about... 50-100 different variables... is THAT wise?

Dimac Grandmaster

 2:27 am on Jul 15, 2004 (gmt 0)

I think either everyone is missing the point, or the original question made little sense. It looks like what you're trying to do is have a single index.php page which loads with a default content page that has a bunch of links that, when clicked, simply load text files or data into the body of index.php. Yes?

This is the basis of most dynamic sites although most people would be accessind the content via a database and not text files.

Essentially your links could be:


Where PageCode calls the relative numbered field from your PageCode table and ID calls same from an Object table that equals PageCode.

Then you'd use some basic SQL to select the content like this:

SELECT * FROM page Where object = $PageCode AND ID = $ID ORDER BY ItemCode

Confusing? Yes, well, no one said this stuff was easy. What you're trying to do cannot be achieved by having someone simply supply you with a 'template'.

Like the rest of us you may just have to do some requisite study of PHP, an RDBMS and the Web server you hope to host your site on. Baby steps.

Good luck!


 2:40 am on Jul 15, 2004 (gmt 0)

I don't want anyone to give me a template! I want someone to show me how, and no that's not what I want, i do'nt see what everyone is confused on lol.

One page with all the html and php code. That's the ONLY PAGE TO HAVE CODE

THEN it loads up content according to what people choose. That's all, one page of code, the rest is the content in .txt files.

Dimac Grandmaster

 2:53 am on Jul 15, 2004 (gmt 0)

Exactly what I described. There is only one index.php page which then pulls in different content from a mySQL database based on the PageCode and ID in the links.


 2:56 am on Jul 15, 2004 (gmt 0)

Then it is very confusing >.< and i have no time for babysteps! I dunno, what about my first post on page 2, what about THAT can u answer those? those are baby steps enough lol.

Dimac Grandmaster

 3:17 am on Jul 15, 2004 (gmt 0)

Echo_Tyme, I really don't see this being answered in a forum thread. You're basically asking someone how to build a dynamic site based on a single index page. A detailed description simply can't be provided here. What you require is a higher understanding of PHP.

In an earlier thread you providied this:

include 'includes/header.php';

$content = "pathtocontentpage.html";
include "template.php";

include 'includes/footer.php';

That is essentially the core elements you need except that obviously all the code needs to be enclosed in <?> tags.

What you need to do now is set your default content for this page. This page would include a list of links, menu bar, whatever, and these links would pull in different content (text files, db fields).

Below is a basic beginning. Please don't ask for more because there is no more. The rest is up to you and depends entirely on how oyu want to structure your site.


include "datasource.inc";

$record = mysql_query("Select * From meta", $conn);
$TotalRecord = mysql_num_rows($record);
$MetaString = "";
for($i = 0; $i < $TotalRecord; $i++){
$MetaString .= mysql_result($record, $i, "Name").",";

if( isset($PageCode) ){
$PageRes=mysql_query("SELECT headtext FROM page WHERE PageCode =$PageCode",$conn);
$PageDescrip="Name of Site Here";
$PageCode="****";} //where xxx=the pagecode for your homepage


 5:04 am on Jul 15, 2004 (gmt 0)

Harry gave me a good thing to go with, and it was quite simple and i'm sure it can be talked down more from what you gave me, i appreciate it but i dunno. I know there is an answer somewhere around here ^.^

Dimac Grandmaster

 5:44 am on Jul 15, 2004 (gmt 0)

If there is be sure to post it here if you ever come across it. I'm sure a lot of people would appreciate it. :)


 6:05 am on Jul 15, 2004 (gmt 0)

Hi Echo_Tyme,

Here's a baby step.

Have a look at this thread.

It basically shows how to use htaccess to rewrite urls. With this whatever url is used gets mapped over to the index file as a variable. So basically you have one index.php file where the $_GET['page'] variable tells you which page to serve up.

I hope this is on track with your concept.



 7:34 am on Jul 15, 2004 (gmt 0)

Unfortunately I forced myself back to the windows 2k3 server, and I hear htaccess is for linux/unix.

Wait a second that thread reminded me of something... My site is about computer maintenance, optimization and security... This means that people will be searching for things off a search engine and stumble upon me... So? So that means I couldn't ahve one page only unless it'd still cooperate, giveng the php include template.php is inplace I guess it would work... I guess i'll have to skip this and try to learn it piece by piece, if I learn the code (that my friend knows) i'll post it immediately...

Now back to what I asked in my first post on page two lol, any answers?

[edited by: Echo_Tyme at 7:37 am (utc) on July 15, 2004]


 7:36 am on Jul 15, 2004 (gmt 0)

Ok, why not use stargeek's method in the same thread?


 7:41 am on Jul 15, 2004 (gmt 0)

Interesting code... I like how $content can be used so widely... Why not jsut use that?

Why not just have a page where the $content is whatever link was clicked, i have NO clue how that would be setup or if it's possible, but I like it.


 10:34 pm on Aug 1, 2004 (gmt 0)

Echo, I am a coder but new to PHP. So I took your thread as a challenge. I understood the following:

1) Create a basic template page that calls the right content dynamically.
2) Store dynamic content as '.txt' files.

These are the 5 files for the test. The actual contents are below. I used dots to indent as blocks would not work for me. Let me know if this is what you were looking for.
1 - index.php : as the main template.
2 - menu.tmpl : as a changeable menu for index.php.
3 - footer.tmpl : as a changeable footer for index.php.
4 - test.txt : as content for the 'Test' menu item.
5 - test2.txt : as content for the 'Test2' menu item.

<?php $menu = '<a href=index.php>Home</a> &nbsp ';
......$menu .= '<a href=index.php?page=test>Test info</a> &nbsp ';
......$menu .= '<a href=index.php?page=test2>Test2 info</a>';

<?php $footer = 'Footer Text';?>

<index.php> - The main template
...$page = $_REQUEST["page"];
...if ($page) {
......$fh = fopen($page . '.txt', "r");
......$contents = fread($fh, filesize($page . '.txt'));
...} else {
......$contents = 'Default content';

...<? echo $menu?><br>
...<? echo $contents?><br>
...<? echo $footer?>

Content for Test.....

Content for Test 2.....


 10:55 pm on Aug 1, 2004 (gmt 0)

Those methods are fine for a first attempt, but they are limited to only the body section being dynamic.

Then when you want a dynamic header (like title tags), you have an include1(), <header content> include2(), <body content>, include3().

A better way to do it is to call ob_start() and start buffering your output.

Then create tags to look for, like %TITLE%, %BODY%, %SIDEBAR%, and so on. Under each section place the contents for that section you want.

Then create a template file, add all variables like %TITLE% in the proper location.

Now at the end of your content, call a single include to a template.php file, which will store the ob buffer text to a string variable. It will then do the same to your template layout. Then break the buffer text into the template sections, and str_replace each item into the proper template location.

It sounds more complicated than it is, but it's only a one-time deal. The template.php file should only be around 12 lines long.


 12:26 am on Aug 2, 2004 (gmt 0)


Sounds like your suggesting a 'Templating System', which is exactly what I wanted to do next. However, I'm having a hard time understanding what you were trying to convey.

Is the index.php file merely a page generator by calling a template.php and filling in embedded variables via some program logic? Or, is index.php the actual template and template.php has the variables as they should be defined for a particular content selection?

Also, am I to use ob_get_contents to get the buffer into a variable before replacing embedded variables?

A little clarification would be great and I will attempt to apply it to the exact same test I posted above and post it here for review.

Thanks so much for your input!


 10:41 pm on Sep 1, 2004 (gmt 0)

Okay, well I worked on it for a while and I've come up with my own templating solution. I have a main script like index.php that calls a main template with tags that need replaced like {TITLE}. I also wanted it to be able to call other scripts that called their own templates.

I created two functions: 1) read_template($page) which reads either another script or an html template file. 2) parse_into(&$PAGE,&$VARS) which takes an associated array of values for replacing into a page.

read_template() uses your technique of ob_start() and ob_get_clean() for buffering into variables.

Here are my functions:

function parse_into(&$PAGE,&$VARS) {
foreach ($VARS as $key => $value) {
$PAGE = ereg_replace("\{$key\}","$value",$PAGE);

function read_template($page) {
if ($page) {
$contents = ob_get_clean();
return $contents;

Here is a fairly complex index.php with serveral scripts and templates being called to complete the page:

// Import my template library
include_once("lib/errorHandler.class.php"); // Error Routines
require_once('lib/myTemplateLib.php'); // Template Library
require_once('mySiteConfig.cfg.php'); // Site Configuration vars

$action = $_REQUEST['a'];

$styles = read_template('tmpl/styles.inc');

$login_bar = read_template('tmpl/login_bar.html');
$vars = array(LOGIN_HREF => $_SERVER['PHP_SELF'] . '?a=signup');

$header = read_template('tmpl/header.html');
$vars = array(LOGIN => $login_bar, HOME_PAGE => $HOME_PAGE);

$submenu = read_template('tmpl/submenu.html');
$vars = array( SUBMENU => buildSubMenu() );

// CONTENT - This is based on the users selection
$content = buildContent($action,$DB['dbh']);

$footer = read_template('tmpl/footer.html');

// MAIN PAGE - Combine all the parts
$main = read_template('tmpl/main.html');
$vars = array(TITLE => 'Dream Of Owning - Own your own home Today!',
STYLES => $styles,
HEADER => $header,
MENU => $submenu,
CONTENT => $content,
FOOTER => $footer);

echo $main;

// Function Definitions

// buildSubMenu()
function buildSubMenu() {
switch ($action) {
return " &nbsp; Home: ";

// buildContent()
function buildContent($action,$link) {

switch ($action) {

case "signup":
return read_template('signup.php');

case "investors":
return read_template('tmpl/investors.html');

case "partners":
return read_template('tmpl/partners.html');

case "contact":
return read_template('tmpl/contact.html');

return "Default Content";

Now, if I can just get this URL Rewriting stuff to work right..... :-)

Thanks for you input!


 1:14 am on Sep 3, 2004 (gmt 0)

Here is my index.php which is use with variation for different folders ...

$inc = $_SERVER["DOCUMENT_ROOT"]."/inc";
include $inc."/setup.php";
include $inc."/header.php";
include $inc."/topnav.php";
include $inc."/left.php";
include $inc."/right.php";
//echo $timestamp.'<br />';
include $inc."/footer.php";

More by request...


 5:42 am on Sep 3, 2004 (gmt 0)

Looks good. Does each of those calls produce html directly mixed with php or is the php and html seperated in a template fashion. I must know more.....


 1:36 am on Sep 4, 2004 (gmt 0)

complete template:

$inc = $_SERVER["DOCUMENT_ROOT"]."/inc";
include $inc."/setup.php";
include $inc."/header.php";
include $inc."/topnav.php";
include $inc."/left.php";
include $inc."/right.php";
//echo $timestamp.'<br />';
include $inc."/footer.php";

$title = 'HomeNew';
$meta_keywords = 'keyword1 keyword2 keyword3';
$meta_description = '3 Column Css ExampleNew';
$style_sheet = 'css/style01' ;
$switch_menu = 'js/switchmenu.js' ;
$page_heading = 'Welcome to 3 Column Css Example';
$top_of_page = 'index.php';

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
<html lang="en">
<title>Foxhunting World >>&nbsp;<?php echo($title);?></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Keywords" content="<?php echo($meta_keywords);?>">
<meta name="Description" content="<?php echo($meta_description);?>">
<link rel="StyleSheet" href="<?php echo($style_sheet);?>.css" type="text/css">
<script src="<?php echo($switch_menu);?>" type="text/javascript"></script>
<div id=outer><!-- end header.php -->

<div class="topnav">
<a href="../index.php">&nbsp;Home&nbsp;</a>
<a href="../membersarea/newmember.php3">Register</a>
<a href="../membersarea/authenticate.php3">Login</a>
left.php //from dynamicdrive switchmenu
<div id="left" class="leftnav">
<!-- Keep all menus within masterdiv-->
<div id="masterdiv">
<div class="menutitle" onclick="SwitchMenu('sub1')">Site Menu</div>
<span class="submenu" id="sub1">
- <a href="new.htm">What's New</a><br>
- <a href="hot.htm">What's hot</a><br>
- <a href="revised.htm">Revised Scripts</a><br>
- <a href="morezone/">More Zone</a>


content.php or pathtofile/filename.php
<?php .
<div id="content">
<!-- Content Here -->

<div id=clearfooter></div><!-- to clear footer --></div><!-- end outer div -->
<div id=footer>footer -
<div id=header><img src="../images/foxhuntingworldmtns.gif" width="800" height="70" alt="Foxhunting World" border="0" align="top"></a>

<div class="topnav">
<a href="../index.php">&nbsp;Home&nbsp;</a>
<a href="../membersarea/newmember.php3">Register</a>
<a href="../membersarea/authenticate.php3">Login</a>

This is a CSS 3 column.
PM me for css and js styling...and demo link

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved