[code]
<?php

class Awf_Template {
	
	/**
	 * The default options
	 *
	 * @var array
	 */
	protected $options = 	array(
								'template_dir' => 'templates/',
								'extract' => true // Do you want to use $var too next to $this->var
							);
	
	/**
	 * Store variable
	 *
	 * @var array
	 */
	protected $vars = array();
	
	/**
	 * Contructor, set some options
	 *
	 * @param array $options
	 */
	public function __construct($options = array()){
		foreach($this->options as $option_key => $value){
			if(!empty($options[$option_key])){
				$this->options[$option_key] = $options[$option_key];
			}
		}
	}

	/**
	 * Set the params
	 *
	 * @param string $key
	 * @param mixed $value
	 */
	public function assign($key,$value){
		$this->vars[(string)$key] = $value;
	}
	
	/**
	 * Fast assiging the variables
	 *
	 * @param unknown_type $key
	 * @param unknown_type $value
	 */
	public function __set($key,$value){		
		$this->assign($key,$value);
	}
	
	/**
	 * This methods will return one variable
	 *
	 * @param string $key
	 * @return mixed
	 */
	public function get_var($key,$default=null){
		$return = $default;
		if(isset($this->vars[(string)$key])){
			$return = $this->vars[(string)$key];
		}
		return $return;
	}
	
	/**
	 * Return all the variables
	 *
	 * @return array
	 */
	public function get_vars(){
		return $this->vars;
	}
	
	/**
	 * Fast syntaxis to return just one varible
	 *
	 * @param string $key
	 * @return mixed
	 */
	public function __get($key){
		return $this->get_var($key);
	}
	
	/**
	 * This method fetches the template file 
	 *
	 * @param string $file
	 * @return string: the parsed file
	 */
	public function fetch($file){		
		$file = $this->options['template_dir'].(string)$file;
		
		if(file_exists($file)){
			ob_start();
			if($this->options['extract']){
				extract($this->vars);
			}
			include $file;
			return ob_get_clean();
		}else{
			throw new Exception('This ('.$file.') template file does not exists');
		}		
	}
	
	/**
	 * This function will echo the parsed template
	 *
	 * @param string $file
	 */
	public function display($file){
		echo $this->fetch($file);
	}
	
	/**
	 * This makes an Awf_Template_Var of the string, so it adds some string modifiers
	 *
	 * @param string $string
	 * @return string
	 */
	public function modify($string){
		return new Awf_Template_VarModifier((string)$string);
	}	
}




class Awf_Template_VarModifier {
	
	protected $var;
	protected $key;
	
	public function __construct($var,$key=null){
		$this->var = $var;
		$this->key = $key;
	}
	
	/*
	 * The modifiers must return themselves to keep chaining possible!
	 *
	 */
	
	public function truncate($lenght=80,$replace='...',$middle=false){
		if(strlen($this->var) > $lenght){
			if(!$middle){
				$this->var = substr($this->var,0,$lenght).$replace;
			}else{
				$this->var = substr($this->var,0,$length/2).$replace.substr($string, -$length/2);				
			}
		}		
		return $this;
	}
	
	public function defaultValue($default=''){
		if(empty($this->var)){
			$this->var = $default;
		}
		return $this;
	}
	
	public function add($value=''){
		$this->var .= $value;
		return $this;
	}
	
	public function entities($quotestyle=null,$charset=null,$double_encode=null){
		$this->var = htmlentities($this->var,$quotestyle,$charset,$double_encode);
		return $this;
	}
	
	public function urlencode(){
		$this->var = urlencode($this->var);
		return $this;
	}
	
	public function indent($chars=4,$char=' '){
		$this->var = preg_replace('!^!m',str_repeat($char,$chars),$this->var);
		return $this;
	}

	public function spacify($spacify_char=' '){
		$this->var = implode($spacify_char,preg_split('//', $this->var, -1, PREG_SPLIT_NO_EMPTY));
		return $this;		
	}
	
	public function __toString(){
		return $this->var;
	}
	
}
?>
[/code]


[b]Voorbeeld:[/b]
index.php
[code]
<?php
$tpl = new Awf_Template;

$tpl->naam = 'Arian';
$tpl->tekst = 'Lorum Ipsum bla bla';

$tpl->display('template.php');

?>
[/code]

templates/template.php
[code]
<p>Hallo, ik heet <?php echo $naam; ?></p>
<p><?php echo $this->modify($this->tekst)->truncate(10)->spacify(); ?></p>

<p><strong>Voledige tekst</strong><?php echo $tekst; ?>
<p>Je kunt dus $this->tekst maar ook $tekst gebruiken</p>

[/code]
