OntoSys  

phpLib examples

Here we present several examples of using the phpLib template class.

A templating approach

We have settled on the following scheme for much of our work generating dynamic web pages with PHP and phpLib.

  1. Put (almost) all HTML code in template files.
  2. Put all PHP code in driver/controller files and related helper files, containing (almost) no HTML code.
  3. Define the overall page layout in a shared template file that comprises a complete HTML document, with template variables and/or blocks for the variable content parts.
  4. Define each individual web page with two files: a PHP driver file, and a template file.
  5. The driver is the entry point for the page (appears in the URL). Written in PHP, it reads the page-specific template, fills the variables in that template with data from the database (or whatever) and merges that content into the shared page template to return the complete page.
  6. The page-specific template is pure HTML. Variable parts and repeatable (or optional) blocks are marked using the phplib template conventions. Note that this template typically comprises a block HTML element that will be incorporated into the content of the body of the complete HTML document.

The rationale for this approach is this:

  1. Since the template files -- shared page layout and page-specific layout -- are pure HTML they can easily be edited using WYSIWYG tools such as Dreamweaver.
  2. Other tools, such as emacs, that do automatic layout of source code don't have to deal with multiple languages (and modes) in the same source file.
  3. There is a very strong separation between HTML/layout, and programmed control and content generation, allowing separate focus on those two distinct concerns. The coupling between the PHP and HTML is very thin, making changes to either one alone easier.

A templating example

page1.php: driver, entry point

<?php
include('template.inc');
$tpl = new Template('.', 'keep');

$tpl->set_file('main', 'layout.html');
$tpl->set_block('main', 'content', 'content1');

$tpl->set_file('content1', 'page1.html');
$tpl->loadfile('content1');

$tpl->set_var('page_title', 'Page 1: a trivial example');
$tpl->set_var('current_date', gmdate("M d Y H:i:s"));

$tpl->pparse('out', 'main');
?>

page1.html: page-specific content

  <p>This content applies only to page1.
    Here is a variable: {current_date}.
  </p>

layout.html: shared page template

<html>
  <head><title>{page_title}</title></head>
  <body>
    <p>This is some content that appears on every page.</p>
    <!-- BEGIN content -->
    <p>Here is some dummy content that is specific to each page.</p>
    <!-- END content -->
  </body>
</html>

Discussion of example

The content block in layout.html could have been a simple template variable instead, but using a block allows us to put some dummy content in place, making it easier to eyeball and tune the layout by itself.

The explicit loadfile() call in page1.php forces the content1 variable to be set from its associated file. The set_file() method merely associates the filename with the variable. Other methods call loadfile() internally when accessing a variable name (such as happens for main in the set_block() call), but that doesn't occur for content1 in this example.

The loadfile() method only reads the associated file if the variable is not yet set. That's why we use both content and content1 rather than just content.

The driver sets variables in both the shared template and in the page-specific block; page_title and current_date, respectively.

Generating a list with separators, using template

list-eg.php: the driver and URL for access

This PHP code uses phpLib template functions to construct a list of items where each item but the first is preceded by a separator. In this example, each item happens to be an HTML 'A' element and the code sets the 'href' attribute and content of each such element.

<?php
include('template.inc');
$template = new Template('.', 'keep');
$template->set_file('main', 'list-eg.html');
$links = array('url1' => 'text1', 'url2' => 'text2', 'url3' => 'text3');
$template->set_block('main', 'each_link', 'all_links');

$separator = '';	// no separator before first item
foreach ($links as $url => $text) {
    $template->set_var('SEPARATOR', $separator);
    $template->set_var('URL', $url);
    $template->set_var('TEXT', $text);
    $template->parse('all_links', 'each_link', true); 
    $separator = '|';	// to use before all subsequent items
}
$template->pparse('out', 'main');
?>

list-eg.html: the template

This fragment of HTML text is the template comprising the list of A elements with separators, all surrounded by brackets. As usual, the template contains only one instance of the list element structure, marked as a "block" by the BEGIN/END comments.

    [
      <!-- BEGIN each_link -->
      {SEPARATOR} <a href="{URL}">{TEXT}</a>
      <!-- END each_link -->
    ]


OntoSys, Inc. - 38W242 Deerpath Rd - Batavia, IL 60510
phone +1.630.879.1312 - fax +1.630.879.1370
email info@ontosys.com - www.ontosys.com
Last modified: Thu, 01 Jan 1970 00:00:00.
Copyright © 1999 - 2004 by Fred Yankowski.
All rights reserved.