6. Object as data

6.1. Substitute of hash

You can use object as substitute of hash, see following code.

  class data {
  	var $title = 'Hello World with Object Property';
  	var $body  = 'ZOGAN is a PHP template engine for XML';
  }
  require_once 'XML/Template/zogan.php';
  $tmpl = new zogan;
  $tmpl->setTemplateFile('./helloworld.html');
  $tmpl->setCompileDir('.');
  $model_data = new data;
  $tmpl->display($model_data);   
      

A property of object is used as if it were a value of hash. Accurately, if there is method of the same name as key of data, the method would be called. Without the method, property of the same name would be used.

6.2. Data itself

By specifying the method to get data from object with addClassForData() method, you can handle objects as data itself. Precisely, if objects of the specified class is given for data, the method is used to get data for template. The following sample is for the template of Hallow World section. The difference is to use "title" object as data.

class title {
	function getTitle() {
		return 'Hello World';
	}
}
require_once 'XML/Template/zogan.php';
$tmpl = new zogan;
$tmpl->setTemplateFile('./helloworld.html');
$tmpl->setCompileDir('.');
$model_data = array(
  'title' => new title,
  'body'  => 'ZOGAN is a PHP template engine for XML'
);
$tmpl->addClassForData('title', 'getTitle');
$tmpl->display($model_data);
      

Class "Zogan" and its method "fetch" is specified by default, so you can use Zogan object as data like this.

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

$tmpl = new zogan;
$tmpl->setTemplateFile('./helloworld.html');
$tmpl->setCompileDir('.');
$model_data = array(
  'title' => 'Including other ZOGAN template Object',
  'table' => $tmpl_table
);
$tmpl->display($model_data);
      

Use setData() method to give date for the template.