XiTemplate Home Page XiTemplate
[ class tree: XiTemplate ] [ index: XiTemplate ] [ all elements ]
Prev Next
XiTemplate Logic

XiTemplate Logic

Writing PHP code for XiTemplate

The following is suitable as a step by step tutorial and will walk you through the process of building your web site with XiTemplate.

Although the following examples deal with building web pages, XiTemplate is also great for building other types of textual documents from templates like email content, XML, etc.

Getting started

The following examples and tutorial assumes the reader has basic knowledge of PHP coding and no knowledge of OOP. If you're already an expert, you can skip forward.

This tutorial was intended to be a step by step process. To participate properly you should fire up your favorite PHP editor and a browser so you can interactively learn XiTemplate as you go.

Instantiating The Class

The first step in getting started with XiTemplate involves instantiating a template object. This is done by calling the class constructor with the new operator.

An object is a collection of capabilities. By creating an object you are creating an instance of a tool that can perform many functions or methods. In essence, you are telling the system to take a tool out of the tool box called XiTemplate and store it in memory for later use.

There is exactly one parameter (argument) that must be passed to the constructor when instantiating the XiTemplate object. There are exactly two options for this argument: 1) the path relative from your script to your template file, or 2) The actual template contents from the buffer.

Before we can instantiate the class we must include the file that contains the class code.

Example:

  1. <?php
  2. /*include the class*/
  3. include_once('../class.XiTemplate.php');
  4.  
  5. /*instantiate the class and pass the path to your HTML template file*/
  6. $xitpl = new XiTemplate('templates/example.tpl');
  7. ?>

Now we have an object known as $xitpl.

If you don't have a template handy here's the one we'll use:

<!-- BEGIN: main -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>XiTemplate</title>
</head>

<body>

<h1>XiTemplate</h1>

<p>Hi. My name  is {NAME}.</p>

</body>
</html>

<!-- END: main -->

Copy and save the above template on your system as "example.tpl" and make sure the path passed to the class constructor is correct.

Next, We parse the page out to the browser.

  1. <?php
  2. /*include the class*/
  3. include_once('../class.XiTemplate.php');
  4.  
  5. /*instantiate the class and pass the path to your HTML template file*/
  6. $xitpl = new XiTemplate('templates/example.tpl');
  7.  
  8. /*parse the "main" block of the page (the whole page)*/
  9. $xitpl->parse('main');
  10.  
  11. /*send the final page out to the browser*/
  12. $xitpl->out('main');
  13. ?>

If you did everything correctly you should have something on the screen. If you don't, make sure the path to the template is correct.

You've probably noticed that the template variable {NAME} was never assigned and that it's blank when the page is parsed. This is a feature we'll discuss as we continue.

Pretty dull so far... stay with me.


Prev Up Next
Template Anatomy (continued) XiTemplate Manual Assign()

Documentation generated on Fri, 20 Aug 2004 10:57:40 -0500 by phpDocumentor 1.3.0RC3