class Template

It is used for rendering active php-templates (php-code mixed with html) and default caching of results processed data  based on the transmitted data.
The class defines the magic methods __ get and __set, the data may be transferred to a template by simply addressing the object property. Inside the template, the data may be addressed the following way: $ this-> someKey.
It is necessary to remember that invoking magic methods in PHP is not very practical, this is why we recommend that you invoke methods like get (), set () and setProperties() directly to ensure the ultimate performance.

class Template {

/**
* Set the template cache manager (system method)
* @param Cache_Interface $manager
*/

static public function setCache(Cache_Interface $manager)

/**
* Template Render
* @param string $path — the path to the template file
* @return string
*/

public function render($path)

/**
* Empty template data
*/

public function clear()

/**
* Disable caching
*/

public function disableCache()

/**
* Enable caching
*/

public function enableCache()

/**
* Get template data
* @return array
*/

public function getData()

/**
* Redefine template data using an associative key-value array,
* old and new data merge
* @param array $data
*/

public function setData(array $data)

/**
* Set template properties
* @param string $name
* @param mixed $value
*/
public function set($name , $value)

/**
Set some template properties
* using an associative array as incoming parameters
* @param array $data
*/
public function setProperties(array $data)

/**
* Get property value
* @param string $name
* @return mixed
*/
public function get($name)

 

}

Example:

<?php

$template = new Template();
// slowly 

$template->page = Page::getInstance();

$template->someData = array('apple','orange','banana');
// 3-4 times faster
$template->setProperties(
array(
‘page’=>Page::getInstance(),
‘someData’=>array('apple','orange','banana')
);
) 

echo $template->render('/path/to/template/tpl.php');

?>


File contents /path/to/template/tpl.php

<h3>Items:</h3>

<ul>

<?php

       if(isset($this->someData) && is_array($this->someData))

             foreach ($this->someData as $item)

                    echo '<li>'.$item.'</li>'

?>

</ul>

comments powered by Disqus