Tutorial Simple php template tutorial

Status
Not open for further replies.
T

theomnis

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.
 

John121

Yellow Belt
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?
 

John121

Yellow Belt
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:
PHP:
<?php
   include('$blocknew')
?>
Is that how it would be?
 

John121

Yellow Belt
Okay.. I need a lil more help. So I would put this:
PHP:
<?php (
   $blocknew=blocknew.html); 
blablablablablablabla include
        ('$blocknew'); 
?>
 
T

theomnis

That looks like it should work. Are you having any problems with it?
 
F

fiverivers

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.
 
Status
Not open for further replies.
Top