Login

PHPLib Templates

PHPLIB templates can grant you an amazing ability to abstract the
manipulation of data (in the database as well as in PHP) from its
final format, whether that format is HTML, XML, WML, or a formatted
e-mail, and some of these ways will be explored here.Hopefully, you’ve read Mr David Orr’s introduction to working with PHPLIB templates. If you haven’t, I recommend that you do so.(http://www.phpbuilder.com/columns/david20000512.php3)

The most common use of templates is to allow you to change the look and feel of a site quickly without having to delve immediately into alot of PHP variable assignments and print statements.

And it’s true that templates allow you to decouple your PHP code and the presentation HTML that’s used to display its results but to limit PHPLIB templates to just this mundane chore would be a terrible disservice.

Used properly, PHPLIB templates can grant you an amazing ability to abstract the manipulation of data (in the database as well as in PHP) from its final format, whether that format is HTML, XML, WML, or a formatted e-mail, and some of these ways will be explored here.

There is usually an argument here about whether FastTemplates is really fast, or faster than PHPLIB, and which is better, etc.

I find that PHPLIB templates are more flexible than FastTemplate, don’t require that you break nested blocks out into multiple files, and I don’t really worry about performance that much, though I’ve been told that PHPLIB is also supposed to be faster, despite the other’s name ‘FastTemplate’.

This can almost get religeous, like arguing about using BSD vs Linux, or MySQL vs PostgreSQL, and I really don’t want to get into that. Try both, and use the one you prefer:FastTemplates http://www.thewebmasters.net/

Hopefully, in either event, this article will be useful to you =)

{mospagebreak title=Simplify} Templates need not be complex. For an example of a simple template, click here. This is a file that can be turned into a template with the greatest of ease – code like the following would easily support this:

This is a template – it allows you to keep your code in one file and the output in another, and allows you to change the template without having to alter (much of) your PHP coding.

This is a most simple example, and only allows you to decouple really basic parts – which is why I bring up the powerful, flexible, and fast PHPLIB template class.

It’s more complicated than the above example for simple variables – but when you get into the more advanced features, you’ll quickly find that the additional complexity is a very small price compared to the benefits gained.

{mospagebreak title=Home is where the heart is} At its heart, PHPLIB’s template class works by defining variables that you stuff information into.

Although variables can be defined in any number of ways, it is important to remember that, once defined, all variables are dealt with in exactly the same way.

As with any programming language, variables contain information of some kind, and most of working with this templates class is defining what the variables are, what you want in them, and how you are going to assemble these variables into some output.

Once again, don’t forget that these variables, once defined, are just logical buckets into which we’ll dump text. Variables consists of strings of text, which can contain still other variables inside of which we can put more text.

This means that variables frequently exist “inside” others, much like directories can be contained in other directories.

{mospagebreak title=Let’s get started} Let’s say you have a file, we’ll call it ‘one.ihtml’, (The name, as well as the extension is completely arbitrary) and the template class is already instantiated inside object variable $T:

one.ihtml:

My name is {username} and I want to tell you {message}.

First off, we need to define a variable for the entire file..

<?
$T->set_file(‘input_one’, ‘one.ihtml’);
?>

And there’s two variables in this file, ‘username’ and ‘message’. We’ll need to define these:

So long as you keep the template block definitions and variable names the same within the source (.ihtml) file, you can use table rows, ordered/unordered lists, BR tags, whatever suits your fancy.

{mospagebreak title=Nested blocks…} It’s not hard to nest blocks inside other blocks. Using this methodology, you can have a single template file that contains any number of logical template ‘pages’. You just have to remember to start from the innermost loop first.

Notice that just before we parse the chapters (the ‘$j’ loop) that we set the ‘PBlock’ variable to ”. That’s because we use the template block more than once, and if we don’t clean the slate, the values from the previous loop will remain.

To see this, just rem out this line and try the code again:

$T->set_var(‘PBlock’, ”);

{mospagebreak title=Aren’t we done YET?} There’s one last detail that I’d like to reinforce that I mentioned earlier.

Within the PHPLIB all variables are equal, even if they aren’t created the same way. This is true of variables defined with set_file, set_var, or set_block!

To demonstrate this, let’s take a look at four.ihtml:

<!– BEGIN BlockOne –>
“I want to know what you think!”
<!– END BlockOne –>
… so the wife says to her husband, {statement}.

You’ll note that the block of text gets put in the right place! Also note this line:

$T->set_var(‘BOne’, ”);

which clears out the BOne variable. This may or may not be necessary depending on how you have PHPLIB configured to run by default.

{mospagebreak title=Closing} Some ideas for using this class:

1) Use a template to to give a site a consistent look and feel. With most sites, you want to have a consistent presentation of links, logos, and other information regardless of what portion of the site they are using. Templating is a drop-dead shoe in.

2) Use templates to allow for rapid and painless rebranding. If you work independantly, you can write your ASP application once, and rebrand the entire site by editing a single template file.

3) Use templates to automatically generate e-mails. This allows you to change your message at any time without diving into code.

4) Use templates for user preferences. A user could change the look and feel of your entire site by choosing the style of his/her choice.

5) Build an XML version of dynamic sites without touching a single bit of PHP code – instead, define a template variable, and use the same block of code with a different template file to generate the HTML or XML version of the site.

And the list goes on…

I hope this has given you some insights on this powerful and useful class.