Template class - KLAR :)

Avdelningen för webbrelaterad programmering och grafisk design.
Post Reply
User avatar
thr
Posts: 1242
Joined: 2002-06-26 22:55:24
Location: Skene
Contact:

Template class - KLAR :)

Post by thr »

Här följer den template class jag har gjort, postade om den för någon vecka sedan. Dock har den blivit omgjord en del, det finns nu stöd för att jobba med templates i 'lager', och detta måste man göra i princip.

Ett exempel:

Code: Select all

$class_template->select_layer(1); /* väljer/skapar lager 1. */
$class_template->set_path("templates/test"); /* sätter sökvägen för mina 
templates i lager 1*/
$array
$class_template->set_template(array("start_template.tpl","main_template.tpl","end_template.tpl")); /* väljer och läser in start_template.tpl, main_template.tpl, end_template.tpl och fogar ihop dem till ett template */
$class_template->set_content("main","Detta är main saken, blabla"); /* lägger till informationen "Detta är main saken, blabla" där {{main}} länken ligger i något av de inlästa templatsen */
$class_template->select_layer(2); /* väljer/skapar lager 2 */
$class_template->set_path("templates/test"); /* sätter sökvägen för mina 
templates i lager 2*/
$class_template->set_template(array("post.tpl")); /* läser in templatet post till lager 2 */
$class_template->set_info("header","rubrik"); /* lägger till informationen rubrik vid {{header}} ankaret i lager 2 */
$compiled_layer2 = $class_template->compile(); /* kompilerar lager 2 och exporterar det till variablen $compiled_layer2 */
$class_template->select_layer(1); /* väljer lager 1 igen */
$class_template->set_content("main",$compiled_layer2); /* lägger till informationen från $compiled_layer2 i till {{main}} i ankaret i lager 1 */
$class_template->compile(); /* kompilerar hela lager 1 */
$class_template->display(); /* visar all information som ligger i den färdigkompilerade lager 1 */
Om det var ett dåligt exempel så kan ni ladda hem hela klassen + ett exempel här:
http://thr.ath.cx/template_test.zip


Här följer klassen

Code: Select all

<?php
// inc/class.template.inc.php
// Showdown Forum
// Licens: GNU/GPL
// By: thr
class template{
	
	var $layers;
	var $current_layer;

	
	function template(){ // constructor
		$layers = array();
	}
	
	function select_layer($layer){ // set which layer to work with
		if($this->current_layer = $layer){
			return true;
		}
	}
	
	function reset_layer($mode="all"){ // used for resetting layer variables
		if($mode == "all"){
			$this->layers[$this->current_layer] = array();
			return true;
		}elseif($mode == "content"){
			$this->layers[$this->current_layer]['content'] = array();
			return true;
		}elseif($mode == "template"){
			$this->layers[$this->current_layer]['template'] = "";
			return true;
		}elseif($mode == "path"){
			$this->layers[$this->current_layer]['path'] = "";
			return true;
		}else{
			echo "<b>reset_layer</b>: allowed optoins are: all, content, template and path<br />";
			return false;
		}
	}

	function set_content($identifier,$content){ // $identifier = identifier in template // $content = content
		if(!is_array($this->layers[$this->current_layer]['content'])){
			$this->layers[$this->current_layer]['content'] = array();
		}
		
		if($this->layers[$this->current_layer]['content'][$identifier] .= $content){
			return true;
		}
	}
	
	function set_path($path){ // set for current layer
		$this->layers[$this->current_layer]['path'] = $path;
		return true;
	}
	
	function set_template($templates){ // must be an array
		foreach($templates as $key => $value){
			$handle = fopen($this->layers[$this->current_layer]['path'] . $value,"r");
			$this->layers[$this->current_layer]['template'] .= fread($handle,filesize($this->layers[$this->current_layer]['path'] . $value));
			fclose($handle);
		}	
		return true;
	}
	
	function compile(){ // compiles the template+info and puts into $compiled array
			$str_buff = $this->layers[$this->current_layer]['template'];
			foreach($this->layers[$this->current_layer]['content'] as $key => $value){	
				$str_buff = preg_replace("/{{" .  $key . "}}/",$value,$str_buff); 			
			}
			$this->layers[$this->current_layer]['compiled'] .= $str_buff;
			return $str_buff;
	}
	
	function display(){
		echo $this->layers[$this->current_layer]['compiled'];
		return true;
	}
	
}

$class_template = new template;
?>
Post Reply