Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

PHP Template, How to Make My Own Template?

How to make my own PHP template instead of using Dreamweaver templates.

8:51 am on Mar 12, 2007 (gmt 0)

Junior Member

10+ Year Member

joined:Nov 17, 2004
votes: 0

I have been using Dreamweaver templates and they are working fine. The problem is that each time I update the template it updates all the files and I have to upload them all to the server each time I make any change to the template.

If I was using PHP instead to enable the template effect, I would need to upload only the template file each time I make a change to it and all other PHP files on the server would just automatically work with the new template.

I know there is Smarty and other template engines, but I want to know how can I create my own template in the simplest form. What the most basic idea behind a template in PHP and how is it made?

10:45 am on Mar 12, 2007 (gmt 0)

Full Member

10+ Year Member

joined:Aug 3, 2005
votes: 0

What i do is design my page, leaving room for where the body text is going to be.
Then cut the template into 2 sections, top and bottom.
Then use includes to pull the html files in to display your template.

<?php include_once("header.html");?>


<?php include_once("footer.html");?>


10:35 pm on Mar 12, 2007 (gmt 0)

Full Member

10+ Year Member

joined:Mar 16, 2004
votes: 0

I do the same....

<? $title = 'My website title';?>
<? include ('page_header.php');?>

HTML goes here

<? include ('page_footer.php');?>


10:37 pm on Mar 12, 2007 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 21, 2005
posts: 1526
votes: 0

The include header/footer pattern is pretty much the industry standard. Almost every CMS out there does this.
12:29 pm on Mar 13, 2007 (gmt 0)


WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
votes: 0

try this library [webmasterworld.com] thread
A dynamic site in 2 minutes [webmasterworld.com]
4:34 pm on Mar 13, 2007 (gmt 0)

Full Member

10+ Year Member

joined:Aug 30, 2002
posts: 303
votes: 0

I usually use a number of includes. Header, Footer, Menu, News etc. All that ftp'ing of dreamweaver templates got to me.
3:54 pm on Mar 14, 2007 (gmt 0)

Junior Member

10+ Year Member

joined:Aug 25, 2003
votes: 0

The include method is great but in addition to this i personaly wanted to be able to inject data into my html templates of which there are:


and if a shop part exists for the site


What i do is use a string replace function that runs through an associative array i.e.

{[page_title]} = My Site
{[site_url]} = www.mysite.com

and replaces the key on the left (in the html.tp.htm file) with the value on the right

In the html template files i do this (just a snippet):


Then my script will fopen the file into a variable then run it through the string replace function to sawp the text entries for actual data, i use this to import company name, page title, current year, menu_data, file_name (actual script being loaded by user) etc into the html template without having to make it part of the core php files of the site, i might even create a function to generate links from a reference {[link_contact]}.

In addition to this, as the page contents are stored in a pagename.content.php file and also entries for each page in an array in a config.inc.php i decided to setup a trigger to parse the contents, i added this to each dataset in the page entry array in config.inc.php, so the script can either fopen the tp.htm file into a var, or if like on my contact page and products page i want to parse some code i can have it include the file (the parseable content files are setup in a certain way) first to parse some code then it will put the resulting data into the content variable for insertion into the tp.htm file where ever needed.

NOTE: unparsed pagename.content.php files should just be raw html no php tags, no html or body tags, just the raw content AND the parseable pages should be wrapped in php tags with the contents stored in a variable (name of which you will decide), this allows for the inclusion of code and as with my contact page i can include error messages in the returned html and auto fill fields if an error occours.

The above is just from something i did this week and im reciting the concept from the top of my head, obviously there are a few other intricacies involved but they relate to menus and settings like selected and going into sub pages/sub categories. but the above textual concept should be a good enough start for someone to produce the same that i have, to lazy to provide a code example but you should be able to get the concept from that and put it to use if you need it.

I've found it extremely usefull now as i can skin sites willy nilly without having to hardcode html into core php files and also my buddy / business partner in crime can update the site templates without having to dip into the code, i'll be incorporating this into the next version of my cms too. The main bonus of this for me was the ability to drop dynamic data into a html template as stated above without hardcoding the html into php core files, infact the core template files are raw html, and its only the content.php files which are php and that is purely to allow for the inclusion of php code for parsing.

This is really inteded at someone who needs an easy way to import a template onto a site which will capture all the data without messing with any php code unless you need to parse something along with a page contents.

Hope someone can put this idea to use like i have.


4:43 pm on Mar 14, 2007 (gmt 0)


WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
votes: 0

seems like a lot of work to include

why not just use vars and a config file to set the vars on each site

stops all the str_replace and fopen work, too much overhead I would think

5:33 pm on Mar 14, 2007 (gmt 0)

Junior Member

10+ Year Member

joined:Aug 25, 2003
votes: 0

I essentially do, just this way if i want to drop a new template in i dont have to mess around with any php code as far as the template goes (other than editing the template folder in the config file) i can just use the data namespace which the script takes care of with regards to replacing them for the actual data as with the example of the page title in the last post.

When it comes to content thats another thing as its deciding weather to include the file and parse code or just to fopen the file into a variable.

(The site uses three config files one for pages one for categories and another for products (the latter two are for the shop section, reason for using flat files is the client aint paying me enough for a database let alone backend)

Didnt take long at all to write and ive tested it with a number of templates on one sites data which works a mighty treat.

If your just after including a static html file with all the links etc set in stone then the above mentioned include methods work perfect, infact thats how i started out but if you want to start inserting dynamic content into a static html file then without the string replace method the only way i can see to do it is have the html files as .php which then require the php variables to be inserted properly like <?php echo $variable?> or <?php $variable?> where required in the html document which then means i cant use shorthand variables as the end user (which may not be me, and may not even know php) might get confused over.

The way ive laid it out in my script is that page_name refers to a variable with a totaly diferent name like for example $_SERVER['page_data']['page_title'] which is just a little long winded for some people i know.

Additionaly the reason for this was to seperate design from content and with that sepereate coders from designers thus i can now employ a designer to create a materpiece, export to a html/css template, cut up the html as required for the system (very stright forward) then with a list of data namespaces can insert content into the new html without even thinking about any coding, changing extension etc, for example in my setup the menu is dynamic and read in from config.inc.php which the outputted html is stored in the data namespace {[main_menu]} (or the variable $_SERVER['page_data']['global_nav']) in a set format of <li><a><span>link</span><a></li>.
So all the chap or lady needs to do is have a file named menu.tp.htm with the content:

<div id="menuContainer">

And {[main_menu]} will after being processed contain the list items and links for the menu, and depending on your setup might even decide to set a link selected, or not.

Bearing in mind the only thing that must stay the same is the <ul>{[menu_container]}</ul> line, everything wrapped around it can change.

APOLOGIES: wasnt fopen i was using it was file_get_contents - had a long couple of days :-s

[edited by: PSWorx at 5:39 pm (utc) on Mar. 14, 2007]