vlibTemplate

vLib Logo

4.4. Modular programming with TMPL_INCLUDE

If you want to create complex designs a modular structure is useful. You can do it with:

  1. TMPL_INCLUDE parameters
  2. several PHP scripts, which all got one template

Which solution you choose, depends on your style of programming. Advantages and disadvantages might differ from one person to the other. I prefer solution 2, because all logic is placed "in the hands" of PHP and your PHP script is one unit, a small part of a big complex website. But perhaps this is possible with solution one, too. First of all I will explain solution 1.

PHP script
   require_once 'vlib/vlibTemplate.php';

   $tmpl = new vlibTemplate('tmpl/modular_tmpl_include.htm');

   $tmpl->setvar('header', 'modular_tmpl_include_header.htm');
   $tmpl->setvar('body', 'modular_tmpl_include_body.htm');
   $tmpl->setvar('title_text', 'TITLE: Modular programming with TMPL_INCLUDE');
   $tmpl->setvar('body_text', 'BODY: Modular programming with TMPL_INCLUDE');

   $tmpl->pparse();

All methods are known and as you can see we divide our template in 2 parts: Header- and Body part, just like the tags <head></head> and <body></body> do it in HTML. What happens here will clarify when we look at the template.

Template: modular_tmpl_include_header.htm
   <tmpl_include file='{var:header}'>
   <tmpl_include file='{var:body}'>

In the PHP script we set "header" and "body". They will include the specified template. Therefore you can make complex navigations with $_GET or get your variables from a database.

Template: modular_tmpl_include_header.htm
   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" 
      "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
   <html>
   <head>
      <title>{tmpl_var name='title_text'}</title>
      <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
   </head>

The template is identical with the first example in this tutorial, but it's seperated in 2 different files. More complex structures are also possible.

Template: modular_tmpl_include_body.htm
   <body>

   <p>{tmpl_var name='body_text'}</p>

   </body>
   </html>