| |
|
 |
Separating content from presentation is much talked about, and the way to solve that has been using one of the many template classes out there.
Files
You'll find the files to this article on the following pages:
PHP Base Lib Credits
template.inc
table_file.tpl
Which Templates?
In the PHP Base Library you'll find one of the earliest and most mature implementations in PHP. You've probably read one or two articles on this issue allready and are starting to ask what more can I learn?
Well, very easy, you can learn how to use block level templates.
If you don't know block level templates you probably know the glut of template files which might explode when you're trying to do serious work with templates. You know one file for each looping output in your code:
table.tpl
tablerow.tpl
The block level templates set out to fix this problem for you by using blocks inside the template file.
A simple template file (table_file.tpl) might look like this, with blocks:
This is a page with a table:
<!-- BEGIN table_one_template -->
<table>
<!-- BEGIN table_row_template -->
<tr>
<td>
{an_item}
</td>
</tr>
<!-- END table_row_template -->
</table>
<!-- END table_one_template --> |
Each BEGIN and END pair starts a block template. So you've just saved yourself a file. Not much but you could argue that an empty table might need an error message, and that would be another template block. Now you've saved two. And whatif you need more tables.
The best benefit is that the designer and you will find it easier to work with one file, it is pretty easy to grasp how things are related in this way.
How do you use this inside your php code? Well, easy enough:
<?php
// Include the file with the class.
include( "template.inc" ); // Remember to add a path if needed.
// Create a new template object, the argument given is the path to the directory
// with your templates.
$tpl = new Template( "." );
// Tell the template object which file you'll be using. "table_file" is a
// handle, "table.tpl" is the file name.
// I suggest you use _file in the handle of the file so that you don't confuse
// it with other handles or variables.
$tpl->set_file( array( "table_file" => "table.tpl" ) );
// Tell the template object about the blocks you'll be using. The function will
// extract the template (arg 2) from the parent (arg 1) and use a variable
// (arg 3) instead.
// "table_file" is the parent of this block, "table_one_template" is the name of
// the block and "table_one" is the variable.
$tpl->set_block( "table_file", "table_one_template", "table_one" );
// "table_one_template" is the parent of this block, "table_row_template" is the
// name of the block and "table_row" is the variable.
$tpl->set_block( "table_one_template", "table_row_template", "table_row" );
for( $i = 0; $i < 10; $i++ )
{
// This will sett the variable "an_item" with the information "This is
// iteration $i" (where $i, of course, is changed for each loop).
$tpl->set_var( "an_item", "This is iteration $i" );
// This will replace the "table_row" variable with all the stuff in
// "table_row_template". The last true says that we should append the new
// content to the allready existing content.
$tpl->parse( "table_row", "table_row_template", true );
}
// This will replace the "table_one" variable with all the stuff in
// "table_one_template". That is, all the stuff we put into "table_row" ++
$tpl->parse( "table_one", "table_one_template" );
// Now just parse and print the whole file.
$tpl->pparse( "output", "table_file" );
?> |
Simple, huh?
A caveat, btw, don't ever try to parse something which don't exist. Well no, you say, but if you try to be smart and use classes and then open only the templates you need before returning to another function which will print it, then you'll probably learn it the hard way.
I did.
On the next page you will find credits and then the template.inc file.
Comment List
There are no comments.
|
 |
|
|