<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* class_template.php
*
* PHP versions 5
*
* Copyright (c) 2008 The ecoware GmbH Project Team
* Licensed under the GNU GPL. For full terms see the file COPYING.
*
* This file is the base class for templates
* it can be used stand-alone or together with class_section.php
* For examples how to use have a look at the included documentation.
*
* @category Template
* @author A. Scholz <hide@address.com>
* @copyright 2008 ecoware GmbH
* @license GNU GPL V2
* @link http://template.ecoware.de
*/
interface iTemplate
{
public function read();
public function assign($holder,$value);
public function auto_assign($data=array());
public function display();
public function get_content();
}
class template implements iTemplate{
// {{{ properties
/**
* Path to the template directory
* @var string
* @access public
*/
public $dir = null;
/**
* The Template file in the template directory
* @var string
* @access public
* @see $dir
*/
public $html = null;
/**
* Set this to true to see unassigned {VARS}
* in your ready parsed template.
* Set to false otherwise.
* @var bool
* @access public
*/
public $keep_unassigned = true;
/**
* The content of the parsed template
* @var string
* @access protected
*/
protected $content = null;
// }}}
/**
* constructor
*
* Example:
*
* <code>
* require_once 'class_template.php';
* $tpl = new template();
* $tpl->dir = 'templates/';
* $tpl->html = 'default.html';
* $tpl->read();
* </code>
*
* @param string $encoding could be iso-8859-1, utf-8, etc..
* @access public
*/
public function __construct($encoding='pass'){
//set this to "1" for debugging only !
ob_implicit_flush(0);
//could be iso-8859-1, utf-8, etc..
mb_http_output($encoding);
ob_start("mb_output_handler");
}
/**
* read
*
* will read in the templeate file
* and invoke the _preparse methode
* Will die if the template could not be found
* or is not readable.
*
* @see $html
* @see $dir
* @access public
*/
public function read()
{
if (!file_exists($this->dir.$this->html))
die ('Error: Template '.$this->dir.$this->html.' not found');
if (!is_readable ( $this->dir.$this->html) )
die ('Error: Template '.$this->dir.$this->html.' not readable');
$this->content= file_get_contents($this->dir.$this->html);
$this->_preparse();
}
/**
* _preparse
*
* will parse the template to find
* values in curly brackets
*
* @access private
*/
private function _preparse(){
preg_match_all('/{.*}/U',$this->content,$matches);
if(empty($matches[0]))
return $this->matches=array();
$this->matches = array_combine($matches[0],$matches[0]);
if(! $this->keep_unassigned){
foreach($this->matches as $key=>$value){
$this->matches[$key] = '';
}
}
}
/**
* assign
*
* Will assign a value to a placeholder
* If you should assign a null value this
* will result in the string 'NULL'.
*
* Example:
*
* <code>
* ....
* $tpl->read();
* $tpl->assign('UNIVERSE','Hello World');
* </code>
*
* @param string $holder A template placeholder
* @param string $value What will be assigned to.
* @return bool true on success, false otherwise
* @access public
*/
public function assign($holder,$value){
if (is_null($value))
$value = 'NULL';
$holder = "{".trim($holder)."}";
if(!isset($this->matches[$holder]))
return false;
$this->matches[$holder]=$value;
return true;
}
/**
* auto_assign
*
* Will assign all values to placeholders
* in this array.
*
* @param array $data An array with placeholder=>value pairs
* @return bool true on success, false otherwise
* @see assign
* @access public
*/
public function auto_assign($data=null){
if(is_array($data)){
foreach($data as $key=>$elem){
if(array_key_exists('{'.$key.'}',$this->matches))
$this->assign($key,$elem);
}
return true;
}
}
/**
* _replace
*
* Will replace all assigned placeholders with the value
*
* @access private
*/
private function _replace(){
$this->output = str_replace(array_keys($this->matches),array_values($this->matches),$this->content);
}
/**
* display
*
* Will echo out the final (html/xml whatsoever) code
*
* @access public
*/
public function display(){
$this->_replace();
echo $this->output;
}
/**
* display
*
* Will return the final (html/xml whatsoever) code
*
* @return string the processed template output
* @access public
*/
public function get_content(){
$this->_replace();
return $this->output;
}
/**
* destructor
*
* Will flush and close the output_buffer
*
* @access public
*/
public function __destruct(){
ob_end_flush ();
}
}
?>