|
|
OntoSys | |
Here we present several examples of using the phpLib template class.
We have settled on the following scheme for much of our work generating dynamic web pages with PHP and phpLib.
The rationale for this approach is this:
<?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');
?>
<p>This content applies only to page1.
Here is a variable: {current_date}.
</p>
<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>
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.
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');
?>
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. |