2. Quick start guide

This section provides you a quick start guide to use ZOGAN version 0.2 or later.

2.1. Hello World

This is a simple template file, which is written with XHTML.

<html>
  <body>
    <h1 var:cont="{title}">title will be inserted here</h1>
    <p var:cont="{body}">body text will be inserted here</p>
  </body>
</html>
   

ZOGAN treats elements with "var:cont" attribute as dynamic parts where data will be embedded. ZOGAN gets the data for template from hash variable with the value of "var:cont" attribute as key. The key in "var:cont" attribute must be enclosed with braces like {foo}. For example,

<h1 var:cont="{title}">...</h1>

will be replaced with

<h1>$model_data['title']</h1>

.

The PHP code to display the template with a hash model data is below. Name the template “helloworld.html” and put it in the same directory with the PHP code, and change the mode of the directory to writable by PHP interpreter, because ZOGAN complies templates to PHP code before display when the templates are new or modified.

require_once 'XML/Template/zogan.php'; // change this to properly path.
$tmpl = new zogan;
$tmpl->setTemplateFile('./helloworld.html');
$tmpl->setCompileDir('.');
$model_data = array(
  'title' => 'Hello World',
  'body'  => 'ZOGAN is a PHP template engine for XML'
);
$tmpl->display($model_data);
   

The output of this code is ...

<html>
  <body>
    <h1>Hello World</h1>
    <p>ZOGAN is a PHP template engine for XML</p>
  </body>
</html>
   

Note

'<', '>', '&', ''' and '"' in data will be escaped to XML entity as default. For more information, see there.

For the permitted characters in key of data, see there.

2.2. List

To repeat one element with multiple data, use array as data to display. The sample is a template for XHTML list, which is named list.html.

<ul>
  <li var:cont="{list1}"></li> // repeat is required in this line. 
</ul>
   

And PHP code for it is

 
require_once 'XML/Template/zogan.php';
$tmpl = new zogan;
$tmpl->setTemplateFile('./list.html');
$tmpl->setCompileDir('.');
$model_data = array(
  'list1' => array(1, 2, 3)
);
$tmpl->display($model_data);
   

When the model data to display, in this case that is 'list1', is array, ZOGAN displays them one by one. So the output will be

<ul>
  <li>1</li>
  <li>2</li>
  <li>3</li>
</ul>
    

Note

PHP do not distinguish array and hash, but normaly array(0 => 'foo', 1 => 'bar') is called array and array('a' => 'foo', 'b' => 'bar') is called hash. So ZOGAN look on PHP array with integer key 0 as array.

2.3. Table

XHTML table can be displayed with nested hash as data. The template is simple like this.

<table border="1">
  <tr><th>name</th><th>author</th></tr>
  <tr var:cont="{table1}">
    <td var:cont="{name}"></td><td var:cont="{author}"></td>
  </tr>
</table>
   

It is possible to nest elements that have "var:cont" attribute.

And the PHP code is

require_once 'XML/Template/zogan.php';
$tmpl = new zogan;
$tmpl->setTemplateFile('./table.html');
$tmpl->setCompileDir('.');
$model_data = array(
  'table1' => array(
    array( 'name' => 'PHP',    'author' => 'Rasmus Lerdorf' ),
    array( 'name' => 'Ruby',   'author' => 'matz' ),
    array( 'name' => 'python', 'author' => 'Guido van Rossum' )
  )
);
$tmpl->display($model_data);
   

The output will be

<table border="1">
  <tr><th>name</th><th>author</th></tr>
  <tr>
    <td>PHP</td><td>Rasmus Lerdorf</td>
  </tr>
  <tr>
    <td>Ruby</td><td>matz</td>
  </tr>
  <tr>
    <td>python</td><td>Guido van Rossum</td>
  </tr>
</table>
   

2.4. Modify attributes

Use {data} style value to embed data in attributes of XML start-tags.

Template:

<table border="1">
  <tr><th>name</th><th>webpage</th></tr>
  <tr var:cont="{table}">
    <td var:cont="{name}"></td>
    <td><a href="{url}" var:cont="{webpage}"></a></td>
  </tr>
</table>
   

Code:

require_once 'XML/Template/zogan.php';
$tmpl = new zogan;
$tmpl->setTemplateFile('./attribute.html');
$tmpl->setCompileDir('.');
$model_data = array(
  'table' => array(
    array( 
        'name'    => 'PHP',
        'url'     => 'http://www.php.net/',
        'webpage' => 'PHP: Hypertext Preprocessor'
    ),
    array( 
        'name'    => 'Ruby',
        'url'     => 'http://www.ruby-lang.org/',
        'webpage' => 'Ruby Home Page'
    ),
    array( 
        'name'    => 'python',
        'url'     => 'http://www.python.org/',
        'webpage' => 'Python Language Website'
    )
  )
);
$tmpl->display($model_data);
   

And output:

<table border="1">
  <tr><th>name</th><th>webpage</th></tr>
  <tr>
    <td>PHP</td>
    <td><a href="http://www.php.net/">PHP: Hypertext Preprocessor</a></td>
  </tr>
  <tr>
    <td>Ruby</td>
    <td><a href="http://www.ruby-lang.org/">Ruby Home Page</a></td>
  </tr>
  <tr>
    <td>python</td>
    <td><a href="http://www.python.org/">Python Language Website</a></td>
  </tr>
</table>
   

Note

Order of attribute for data against var:cont attribute is important. For more information, see here.