Location: PHPKode > projects > Simple Way to Usenet > libs/core/parser/template.class.php
<?php
/**
 *  
 * @author  Benjamin Gillissen <hide@address.com>
 * 
 *	**************************************************************

	Copyright (C) 2009  Benjamin Gillissen
	
	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details at:
	http://www.gnu.org/copyleft/gpl.html

 *	**************************************************************
 */
class template {
	
	private $HDL;
	private static $STAGE1;
	private $context;
	private $parser;
	private $outfunc='template::dftoutput';
	 
	
	public function __construct(){ }
	
	public function __destruct(){
		if ( isset($this->context) ){ $this->releasedata();	}
	}
	
	public function output_function($func){ $this->outfunc = $func;	}
	
	public static function dftoutput($str){ echo $str; }
	
	public function load_file($path){
		if ( FALSE === $path ){ return FALSE; }
		if ( !chrono::isinit("tpl_load") ){ chrono::init("tpl_load"); }
		$chr = chrono::start("tpl_load");
		$tplrepo = themes::get_active_conf('tpl');	//active theme template folder, can be multiple folder 
		//$tplrepo .= PATH_SEPARATOR.PATH_LIBS;					//libs folder maybe some tpl in there.
		//$p = CORE::getdeps($tplrepo, $path);
		$p = $tplrepo.'/'.$path;
		if ( FALSE === $p ){ 
			errors::raise('Missing template file : '.$path.' was not found in '.$tplrepo, CORE_LOG_ERROR, 'TPL');
			$o = FALSE;
		} else {
			unset($tplrepo, $path);
			$o = $this->load($p);
		}
		chrono::pause("tpl_load", $chr);
		return $o;
	}
	
	public function load_string($str){
		//FIXME TODO, not usefull for now.
		$path = CORE::tempfile(); 
		//put str to tmp file
		return $this->load($path);
	}
	
	private function load($p){
		if ( !is_readable($p) ){ 
			errors::raise('Template file : '.$p.' is not readable.', CORE_LOG_ERROR, 'TPL');
			return FALSE; 
		}
		$hash = CORE::hash($p);
		$o=TRUE;
		if ( !isset(self::$STAGE1[$hash]) ){
			self::$STAGE1[$hash]['hdl'] = fopen($p, 'r');
			errors::raise('Loading template file : '.$p, CORE_LOG_DEBUG, 'TPL');
			self::$STAGE1[$hash]['obj'] = new tpl_parser(self::$STAGE1[$hash]['hdl'], 0, filesize($p), 0, $this->outfunc);
			$o = self::$STAGE1[$hash]['obj']->detect_block();		
		}
		$this->parser = &self::$STAGE1[$hash]['obj'];
		return $o;
	}
	
	private function loadatacontext(){
		if ( !isset($this->context) ){ $this->context = tpl_data::newcontext(); }
	}
	
	public function setdata($ar){
		$this->loadatacontext();
		return tpl_data::setdata($this->context, $ar); 
	}
	
	public function appendata($ar, $k){
		$this->loadatacontext();
		return tpl_data::appendata($this->context, $ar, $k); 
	}
	
	public function releasedata(){
		$this->loadatacontext();
		tpl_data::clearcontext($this->context);
		unset($this->context);
	}
	
	public function parse($return=FALSE){ 
		if ( !isset($this->parser) ){
			errors::raise('You must load a template before parsing it eh !', CORE_LOG_ERROR, 'TPL'); 
			return FALSE; 
		}
		if ( !chrono::isinit("tpl_parse") ){ chrono::init("tpl_parse"); }
		$chr = chrono::start("tpl_parse");
		$o = $this->parser->parse($this->context, NULL, $return);
		chrono::pause("tpl_parse", $chr);
		return $o; 
	}

	public function parse_before($bref, $return=FALSE){ 
		if ( !isset($this->parser) ){
			errors::raise('You must load a template before parsing it eh !', CORE_LOG_ERROR, 'TPL'); 
			return FALSE; 
		}
		tpl_parser::resetdetect($bref);
		return $this->parser->parse_before($this->context, NULL, $return, $bref); 
	}
	
	public function parse_once($bref, $data, $key, $return=FALSE){ 
		if ( !isset($this->parser) ){
			errors::raise('You must load a template before parsing it eh !', CORE_LOG_ERROR, 'TPL'); 
			return FALSE; 
		}
		tpl_parser::resetdetect($bref);
		return $this->parser->parse_once($this->context, NULL, $return, $bref, $data, $key);
		
	}
	
	public function parse_after($bref, $return=FALSE){ 
		if ( !isset($this->parser) ){
			errors::raise('You must load a template before parsing it eh !', CORE_LOG_ERROR, 'TPL'); 
			return FALSE; 
		}
		tpl_parser::resetdetect($bref);
		return $this->parser->parse_after($this->context, NULL, $return, $bref);
	}
	
	public function parse_between($sbref, $ebref, $return=FALSE){ 
		if ( !isset($this->parser) ){
			errors::raise('You must load a template before parsing it eh !', CORE_LOG_ERROR, 'TPL'); 
			return FALSE; 
		}
		tpl_parser::resetdetect($sbref);
		tpl_parser::resetdetect($ebref);
		return $this->parser->parse_between($this->context, NULL, $return, $sbref, $ebref);
	}
}


Return current item: Simple Way to Usenet