Results 1 to 10 of 10
Thread: Simple PHP Template Tutorial
-
03-29-2005 03:19 PM #1theomnis Guest
Simple PHP Template Tutorial
I am currently using this method on one of my sites, and I thought I would share it with you. If you have an in-depth knowledge of PHP, then this tutorial probably isnt for you, seeing as there are more efficient ways of doing the same thing. However, if you are just getting into PHP or dont know much PHP.
The following method of using PHP templates is useful if you have alot of pages that use the same design. It makes it so that if you want to make a layout change, or change a menu in your site, you do not have to update numerous HTML files. You can simply edit one PHP file.
Firstly, you will need a PHP file to use as a template. This will be your layout. Name it "template.php", or something else if you really want to. This is where most of the site design will be done. Simply make a layout, and then where you want the content section of the page to be, put the following PHP code:
<?php
include($pagename);
?>
This makes it so that the section of the site you put the code in will be filled with the specified file, which in this case will be an HTML file. Also, make sure that any links to other sections of your site link to a PHP file, like "contact.php" or "downloads.php".
You can view a sample PHP template file at http://www.gameglyph.com/template.php.
Ok, now back to those other PHP files. Let's assume that "contact.php" will be your page. In "contact.php" put the following code:
<?php
$pagename = "contact.html";
require_once("template.php");
?>
This will set the variable $pagename to the corresponding HTML file which would be where you actually put the page's content. It will then load "template.php" which will see that you have defined $pagename, and load that HTML file.
So, in conclusion, each page is made up of three files.
PHP Template File - "template.php" - This is the general layout and unchanging areas of your site.
PHP Page File - "contact.php" - This is the file that loads the template, and tells the template which HTML file to put in the designated content section.
HTML Content File - "contact.html" - The corresponding HTML file that is used as the actual content of the page.
Again, this is just a basic method. If you are using "index.php" you will either have to name the corresponding HTML file to something like "home.html" or just change your site indexing priorities to make it so that PHP index files come before HTML files. Also, if you want to use a PHP file as the content page, you can just use "contact2.php" or something to that extent.
A functional example of this method can be seen at http://www.gameglyph.com. If you have any questions or comments, please feel free to reply.
-
03-29-2005 03:37 PM #2
Senior Member
Array
- Join Date
- Mar 2005
- Posts
- 184
Hey theomnis! thanks a billion man.. I needed that. I know very little php and I struggle with it.
So when you are putting that code it, you just place that strip of code where ever you want that "item" to come up at?
-
03-29-2005 03:49 PM #3
Member
Array
- Join Date
- Mar 2005
- Posts
- 41
Hmmm.. Interesting way of doing things.
Thanks for posting it.
And John: Yes.
-
03-29-2005 04:00 PM #4
Senior Member
Array
- Join Date
- Mar 2005
- Posts
- 184
Okay so say you wanted to add a block to your site. You would creat the design in save it in an html file... then you'd find where you want to put it then you'd say:
Is that how it would be?PHP Code:<?php
include('$blocknew')
?>
-
03-29-2005 04:13 PM #5
Member
Array
- Join Date
- Mar 2005
- Posts
- 41
Im pretty new to PHP, but yeah, that looks right.
-
03-29-2005 04:18 PM #6
Senior Member
Array
- Join Date
- Mar 2005
- Posts
- 184
Okay.. I need a lil more help. So I would put this:
PHP Code:<?php (
$blocknew=blocknew.html);
blablablablablablabla include
('$blocknew');
?>
-
03-29-2005 05:33 PM #7theomnis Guest
That looks like it should work. Are you having any problems with it?
-
03-29-2005 07:37 PM #8
Senior Member
Array
- Join Date
- Mar 2005
- Posts
- 184
Nah its workin
Thanks pal :)
-
03-30-2005 02:39 PM #9
Senior Member
Array
- Join Date
- Mar 2005
- Posts
- 161
Lol, there goes John always figuring stuff out
-
03-27-2012 07:54 AM #10
Banned
Array
- Join Date
- Feb 2012
- Posts
- 67
Thanks alot for putting this message really very informatics I know very little php and I struggle with it. So please also update the code it.
Similar Threads
-
Parham's PHP Tutorial
By Ardenn in forum Website Programming DiscussionReplies: 15Last Post: 07-19-2012, 10:56 AM -
[WTS] 25 Simple Wordpress Template Collection
By SenseiDesign in forum TemplatesReplies: 2Last Post: 03-16-2009, 09:13 PM -
Free Link Bid Script Red Template
By temi in forum Webmaster MarketplaceReplies: 0Last Post: 09-19-2008, 08:14 AM -
PHP template problem
By RiverWire in forum Website Programming DiscussionReplies: 2Last Post: 08-23-2007, 07:45 PM -
PHP Security <- A MUST READ
By alexandru in forum Website Programming DiscussionReplies: 0Last Post: 01-19-2005, 01:59 PM



LinkBack URL
About LinkBacks



Bookmarks