faqts : Computers : Programming : Languages : PHP : Common Problems : Templates

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

4 of 4 people (100%) answered Yes
Recently 4 of 4 people (100%) answered Yes

Entry

vlibTemplate - How do I separate data (PHP) from design (HTML)?

Sep 8th, 2002 08:10
Claus VB, Kelvin Jones (http://vlib.activefish.com/)


vlibTemplate is a php class that is intended to make splitting PHP from 
HTML a simple and natural task. 

It makes use of the following vlibTemplate markup tags (like html tags):

<tmpl_var>, <tmpl_loop>, <tmpl_include>, <tmpl_phpinclude>, 
<tmpl_comment>, <tmpl_if>, <tmpl_elseif>, <tmpl_else>, <tmpl_unless>, 
<tmpl_endloop>, <tmpl_endcomment>, <tmpl_endif> and <tmpl_endunless>. 

The file written with these style tags is called a template. A template 
can be an HTML file to use on the web, or perhaps a text file to use as 
an e-mail template... as you can guess there are many many 
possibilities. 

The template file is always seperate from the php script that uses it, 
that way, a designer for example can change the template file without 
having to go through all of the php coding, 
thus saving the developer having to worry about it.
Using this class you set the values for the variables, loops, if 
statements ..etc which are declared in the template. This enables you 
to seperate all of the design from the data, which you create using PHP.

vlibTemplate has three user accessible classes. vlibTemplate, 
vlibTemplateDebug and vlibTemplateCache. vlibTemplate is the main class 
which handles all of the passing and has the majority of the functions. 
vlibTemplateDebug and vlibTemplateCache both extend vlibTemplate and 
inherit it's functionality.
You can therefore change the functionality of vlibTemplate just by 
appending Debug or Cache.
The debug class requires no parametres to be set, it prints out the 
vlibTemplate debug module with debug data for the current page. The 
cache class has a few extra functions that are utilisable, the class 
saves the parsed file into the filesystem to allow much faster page 
accesses and a faster site.

Simple example:

vlibTemplate_basic.php
<?php
    include_once('../vlibTemplate.php');

    $tmpl = new vlibTemplate('templates/vlibTemplate_basic.html');

    $tmpl->setVar('title', 'This is the vlibTemplate Basic example...');
    $tmpl->setVar('msg', 'This is the message set using setVar()');

    $tmpl->pparse();
?>

vlibTemplate_basic.html
<html>
<head>
<title><tmpl_var name='title'></title>
</head>

<body>
<tmpl_var name='msg'>
</body>
</html>