PEAR Manual
Prev |  | Next

# Introduction

Introduction -- Creating and Parsing templates

## Placeholder

The format of placeholder is

    {[0-9A-Za-z_-]+}

This means, the name of the placeholder can consist of upper- and lowercase letters, underscores and hypens. The name must be placed between curly brackets without any spaces. Valid names are i.e.:

{Placeholder}
{place2_holder}
{PLACEHOLDER1}
{Place-Holder}

Non-valid names are i.e.

{ Placeholder 3 } (spaces)
{place*holder} (char isn't permitted)

## Blocks

The format of a block is

    <!-- BEGIN [0-9A-Za-z_-]+ -->
    ... block content ...
    <!-- END [0-9A-Za-z_-]+ -->

The rules for the block name are the same like for placeholders. In contrast to placeholders the spaces in the block markup are required.

The nesting of blocks is permitted, but be careful while parsing. You have to set and parse the deepest inner block first and then set and parse from inner to outer.

In IT the whole template file itself is nested in a virtual block called "__global". Most block-related functions use this block name as default.

## Usage Example

Example 41-1. The template
    <html>
     <table border>
    <!-- BEGIN row -->
      <tr>
    <!-- BEGIN cell -->
       <td>
        {DATA}
       </td>
    <!-- END cell -->
      </tr>
    <!-- END row -->
     </table>
    </html>

Example 41-2. The script
    <?php
      require_once "HTML/Template/IT.php";

      $data = array
      (
        "0" => array("Stig", "Bakken"),
        "1" => array("Martin", "Jansen"),
        "2" => array("Alexander", "Merz")
      );

      $tpl = new HTML_Template_IT("./templates");

      $tpl->loadTemplatefile("main.tpl.htm", true, true);

      foreach($data as $name) {
        foreach($name as $cell) {
            // Assign data to the inner block
            $tpl->setCurrentBlock("cell") ;
            $tpl->setVariable("DATA", $cell) ;
            $tpl->parseCurrentBlock("cell") ;
        }
        // Assign data and the inner block to the
        // outer block
        $tpl->setCurrentBlock("row") ;
        $tpl->parseCurrentBlock("row") ;
      }
      // print the output
      $tpl->show();

    ?>

Example 41-3. The output
    <html>
     <table border>
      <tr>
       <td>
        Stig
       </td>
       <td>
        Bakken
       </td>
      </tr>
      <tr>
       <td>
        Martin
       </td>
       <td>
        Jansen
       </td>
      </tr>
      <tr>
       <td>
        Alexander
       </td>
       <td>
        Merz
       </td>
      </tr>
     </table>
    </html>

Prev | Home | Next
HTML_Template_IT | Up | HTML_Template_IT::get()