Scripts

Template Class

Deze class maakt gebruik van de PHP parser, dus lekker snel en je hoeft geen 'extra taaltje' te leren net als bij smarty. Ook is er de mogelijkheid om strings wat aan te passen met de modifier class en zijn methods: door simpel de modifier class aan een string te binden met $this->modifier('string'); in je template file. De modifier methods zijn ook chainable, zodat je $this->modifier('string')->truncate()->entities(); kunt doen. De methods returnen dus hun eigen instantie... Je kunt ook instellen of je de extract() functie in de fetch() method (van Awf_template class) wilt gebruiken (standaard doet hij dat wel) zodat je niet echo $this->var; hoeft te typen, maar ook gelijk echo $var; kunt gebruiken. Helemaal onderaan staat een voorbeeldje hoe je het kunt gebruiken. Naast alleen strings, kan je natuurlij ook gewoon arrays, objecten, boolean, gewoon elk soort variabele doorgeven. De modifier method werkt echter alleen op strings/integers (dus niet met arrays/objecten).

template-class
[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]

Reacties

0
Nog geen reacties.