Location: PHPKode > scripts > Skor PHP Template > skor-php-template/class_section.php
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

	/**
	* class_section.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 extends the class_template.php
	* it can not be used stand-alone 
	* 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 iSection
{
	public function read();
	public function fetch($section=false);
	public function assign_section($csection);
	public function section_assign($csection);
	public function get_content();
	public function display();
}

class section extends template implements iSection{
	
	// {{{ properties

	/**
	* Sections from template
	* @var array
	* @access protected
	*/
	protected $sections  = null;
	
	// }}}

	/**
	* constructor
	*
	* Example:
	*
	* <code>
	* require_once 'class_template.php';
	* require_once 'class_section.php';
	* $tpl = new section();
	* $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'){
		$this->_encoding = $encoding;
	}
	
	/**
	* read
	*
	* will read in the templeate file
	* calls the parent's constructor 
	* and read methode to get all needed
	* data. When done parse for sections.
	*
	* @see template:$html
	* @see templae:$dir
	* @access public
	*/
	public function read(){
		
		parent::__construct($this->_encoding);
		parent::read();
		$this->_parse_sections();	
	}
	
	/**
	* _parse_sections
	*
	* will parse the template to find
	* sections  within html comments
	*
	* @see read()
	* @access private
	*/
	private function _parse_sections(){
	
		$regex='/<!--section-start::(.*)-->(.*)<!--section-end::(\1)-->/Uis';
		$sections=preg_match_all($regex,$this->content,$secs);
		$this->sections = array_flip($secs[1]);
		$this->sections_content=$secs[2];
	}
	
	/**
	* fetch
	*
	* Example:
	*
	* <code>
	* $var = $tpl->fetch('SectionName');
	* </code>
	*
	* Will return the content of the given section
	* Will die if no section was passed or the section
	* do not exist.
	* 
	* @param string $section The section's name 
	* @return string section content
	* @access public
	*/
	public function fetch($section=false){
		
		if(!$section)
			die('No section given');
		
		if(!isset($this->sections[$section]))
			die ( 'No such Section : ' . $section );

		$this->_sreplace();

		return trim($this->temp[$this->sections[$section]]);
	}

	/**
	* _sreplace
	*
	* Will replace all assigned placeholders with the value
	* in the current section.
	*
	* @access private
	*/
	private function _sreplace(){
		
		$this->temp=str_replace(array_keys($this->matches),
		array_values($this->matches),$this->sections_content);
	}
	
	/**
	* assign_section
	*
	* Will assign a section
	* Will die if there is no such section
	* calls the parent's .display methode
	*
	* This will echo out the section given
	* so do not call this if you simply wish
	* to fetch a section.
	*
	* Example:
	*
	* <code>
	* $tpl->assign_section('SectionName');
	* </code> 
	*
	* @param string $section The section's name 
	* @see fetch()
	* @access public
	*/
	public function assign_section($csection){
	
		if(!in_array($csection,$this->sections))
			die('Section:'. $csection .' does not exist');
			
		$this->content = $this->sections_content[$this->sections[$csection]];
		
		parent::display();
	}

	/**
	* section_assign
	*
	* wrapper for assign_section 
	*
	* @see assign_section
	* @access public
	*/
	public function section_assign($csection){
		$this->assign_section($csection);
	}
	
	/**
	* get_content
	*
	* will return all assigned sections
	* and clean the buffer.
	* Calling display after this methode
	* will result in a empty screen
	*
	* @return string Processed template content
	* @see fetch()
	* @see display()
	* @access public
	*/
	public function get_content(){
		$tmp = ob_get_contents() ;
		ob_clean();
		return $tmp;
	}
	
	/**
	* display
	*
	* Will flush the output buffer
	* The template's content will
	* be send to the browser
	*
	* @see fetch()
	* @see display()
	* @access public
	*/
	public function display(){
		ob_flush();
	}

	/**
	* destructor 
	*
	* Will flush and close the output_buffer
	*
	* @access public
	*/
	public function  __destruct(){
		ob_end_flush ();
	}
}
?>
Return current item: Skor PHP Template