Example Usage
The following examples include PHP code, an HTML template, and the
resulting HTML after parsing.
Example PHP Code
- <?php
- /*include the class*/
- include_once('../class.XiTemplate.php');
-
- /*instantiate the class and pass the path to your HTML template file*/
- $xitpl = new XiTemplate('templates/assign.tpl');
-
- /*assign a value to a template variable.*\
- /*In this example {NAME} will be replaced with "XiTemplate"*/
- $xitpl->assign('NAME', 'XiTemplate');
-
- /*multidimensional array*/
- $myArray = array
- (
- 'name' => 'Jim',
- 'lastName' => 'Grill',
- 'address' => array
- (
- 'street' => 'One XiTemplate Way',
- 'city' => 'Paradise',
- 'state' => 'Hawaii',
- 'zip' => '12345'
- ),
- 'age' => 'too old'
- );
-
- /*assign the array to a template variable*/
- $xitpl->assign('MYARRAY', $myArray);
-
- /*parse the "main" block of the page (the whole page)*/
- $xitpl->parse('main');
-
- /*send the final page out to the browser*/
- $xitpl->out('main');
- ?>
Example Template Code
<!-- BEGIN: main -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>XiTemplate Manual - assign()</title>
</head>
<body>
<h1>XiTemplate</h1>
<p>Hi. My name is {NAME}.</p>
<p>This example was written by:</p>
<p>{MYARRAY.name} {MYARRAY.lastName}<br>
{MYARRAY.address.street}<br>
{MYARRAY.address.city}, {MYARRAY.address.state} {MYARRAY.address.zip}
</p>
</body>
</html>
<!-- END: main -->
Example Resulting HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>XiTemplate Manual - assign()</title>
</head>
<body>
<h1>XiTemplate</h1>
<p>Hi. My name is XiTemplate.</p>
<p>This example was written by:</p>
<p>Jim Grill<br>
One XiTemplate Way<br>
Paradise, Hawaii 12345
</p>
</body>
</html>
The preceding example is very basic and is not at all meant as an
exhaustive example of what XiTemplate is capable of.
Please continue reading for a better understanding of XiTemplate's
features.