PHP,article,explains,personal,simple,approach
Quick Search for:  in language:    
PHP,article,explains,personal,simple,approach
   Code/Articles ╗ |  Newest/Best ╗ |  Community ╗ |  Jobs ╗ |  Other ╗ |  Goto ╗ | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
PHP Stats

 Code: 66,707 lines
 Jobs: 14 postings

 
Sponsored by:

 

You are in:

 
Login



Latest Code Ticker for PHP.
Simple Forum
By Chas Pardee on 6/20


Click here to see a screenshot of this code!Squiggles
By Chas Pardee on 6/20

(Screen Shot)

photo guide
By Chas Pardee on 6/20


Download Script
By YANP on 6/19


Logging IP's
By Klaus Andersen on 6/18


Server Uptime
By Martin C. Conniffe on 6/15


Send a Page/Link to a Friend
By rae the coder on 6/15


Display contents of a directory and include it in a text area
By Daniel Friedman on 6/15


RaSMail
By Alberto Sartori on 6/13


Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!





Affiliate Sites



 
 
   

Using templates with PHP

Print
Email
 

Submitted on: 7/30/2001 7:36:08 AM
By: Gianluca Baldo  
Level: Intermediate
User Rating: By 5 Users
Compatibility:PHP 3.0, PHP 4.0

Users have accessed this article 4581 times.
 

(About the author)
 
     The article explains a personal and simple approach to the use of templates in PHP scripts.

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
1) You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for langauges that allow it) freely and with no charge.   
2) You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
3) You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
4) You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.

Using templates with PHP

We will assume you are planning to develop a PHP application.
First question: what are templates?

Templates are text files usually containing HTML code and "some tags" to fill the HTML with dinamically generated content. Mantaining your PHP code separated from the files responsable for the visual part of your site, will give the ability to change your site's look & feel in a simple and fast way in the future, without having to touch you PHP code.

Embedding HTML in your PHP is not a good idea. The more you keep your HTML independent the easier it will be to maintain you site.
An approach like the following, having a PHP file which outputs the HTML tags (we assume you have to output the content of the variable $test):

[some HTML here] $test [some more HTML]

"; ?>

is not convenient since changes to your HTML will require that you edit your PHP code. This approach can be somehow confusing and not confortable.
A better solution could be:

[you HTML here]

[some more HTML]

which is not a template yet, but we are beginning to separate the HTML tags from PHP code.
If you need to perform some actions before your to give the variable $test the correct value (i.e. accessing a database table and reading a column value) you'll have to add the necessary PHP code, for example at the very top of the file. Something like:

// We assume your are running MySQL $query = "SELECT myvalue FROM test_table";
$result = mysql_query($query);
$test = mysql_result($result,0,"myvalue"); ?> [you HTML here]

[some more HTML]

So we now have a unique PHP file where we intend to keep HTML tags and PHP code separated. This is simpler to mantain and will be clearer to any other who will have to edit and/or mantain your script.
HTML tools like Dreamweaver can recognize PHP tags and will not alter your PHP code. I use to develop PHP applications where the visual part is rather complex and Dreamweaver is a good tool to help in the production process.
So if your WYSIWYG HTML editor do not alter PHP code, you can open your script and just make the necessary changes to the HTML. The PHP will be generally shown as an icon and you can simply move the code behind that icon (if necessary) moving the icon itself.

This sounds good enough, so: why should you use templates?
Let's see how templates works (well, the way I use templates) and this will probably answer the question.

First let's create the tamplate file (i.e. sample_template.html). It will contain HTML tags we need to create the visual part of our script.

[you HTML here]

After that, let's create the PHP file which will contain the PHP code we need:

// We assume your are running MySQL $query = "SELECT myvalue FROM test_table";
$result = mysql_query($query);
$test = mysql_result($result,0,"myvalue"); include "sample_template.html"; ?>

We now need to make the last change to our HTML file:

[you HTML here]

What we basically did is to separate the PHP code and the HTML in two different files. The PHP script file performs the necessary actions and, once done, includes the template (HTML) file.
This is really better than the "one-file" solution since the HTML is clear and even graphic designers can open the template files and make visual changes to them, being sure they will not accidentally alter the PHP behind it.

There are many approaches to the templates idea, the one I presented here is the one I found more confortable after using some of them.
Here are some reference if you want to read something more about templates.

Templates - why and how to use them in PHP3 (by Sasha Shumann)
Templates, The PHPLIB Way (by David Orr )
Using Templates (by Todd Williams)
Includes and Templates with PHP


Other 2 submission(s) by this author

 

 
Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
Your Vote!

What do you think of this article(in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor See Voting Log
 
Other User Comments
2/15/2002 6:27:53 PM:Unix Exploits
makes no sense why you would post this, useless...
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this article has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular article, please click here.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | PHP Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyrightę 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.