All pastes #595791 Raw Edit

Miscellany

public text v1 · immutable
#595791 ·published 2007-06-29 17:40 UTC
rendered paste body
<?php

class MGTheme
{
	public  $home 		= '';
	public  $files  	= array();
	private $data_var 	= array();
	private $data_blk 	= array();
	private $data_emb 	= array();
	private $tpl_result;
	
	public function __construct( $home = "./" )
	{
		if(! is_dir($home) )
		{
			trigger_error( 'Theme: Error al abrir el directorio de templates.', E_USER_ERROR );
		}
		
		$home = substr($home, -1) != '/' ? $home . "/" : $home;
		
		$this->home = $home;
	}
	
	public function load( $files )
	{
		if (! is_array($files) )
		{
			return false;
		}
		
		foreach( $files as $id => $name )
		{
			$this->files[$id] = $this->home . $name;
			if( ! file_exists( $this->files[$id] ) )
			{
				trigger_error( "Theme::load(): El archivo " . $this->files[$id] . " no existe.", E_USER_ERROR );
			}
		}
		
		return true;
	}
	
	public function parse( $file )
	{
		if (! $this->files[$file] )
		{
			trigger_error( "Theme::parse(): No existe archivo asignado a " . $file, E_USER_ERROR );
		}
		
		$this->checkEmbedded();
		
		print $this->compile( $this->files[$file] );
	}
	
	public function assignEmbedded( $var, $file )
	{
		if (! $this->files[$file] )
		{
			trigger_error( "Theme::assignEmbedded(): No existe archivo asignado a " . $file, E_USER_ERROR );
		}
		
		$this->files_emb[$var] = $file;
		
		return true;
	}
	
	public function assingBlock( $block, $values )
	{
		if ( preg_match("/\./", $block) )
		{
			$blocks = explode( '.', $block );
			$blockcount = sizeof( $blocks ) - 1;
			$str = '$this->data_blk';
			for ($i = 0; $i < $blockcount; $i++)
			{
				$str .= '[\'' . $blocks[$i] . '\']';
				eval('$lastiteration = sizeof(' . $str . ') - 1;');
				$str .= '[' . $lastiteration . ']';
			}
			$str .= '[\'' . $blocks[$blockcount] . '\'][] = $values;';
			
			eval( $str );
		}
		else
		{
			$this->data_blk[$block][] = $values;
		}
		
		return true;
	}
	
	public function assignVars( $values )
	{
		foreach( $values as $var => $value )
		{
			$this->data_var[$var] = $value;
		}
		
		return true;
	}
	
	public function assignVar( $var, $value )
	{
		$this->data_var[$var] = $value;
		
		return true;
	}
	
	private function checkEmbedded()
	{
		if(! empty( $this->data_emb ) )
		{
			foreach( $this->data_emb as $var => $file )
			{
				$data = '';
				$data = $this->compile( $this->files[$file] );
				$this->assignVar($var, $data);
			}
		}
		
		return true;
	}
	
	private function _includeTpl( $match )
	{
		return file_exists( $this->home . $match[ 1 ] ) ? file_get_contents( $this->home . $match[ 1 ] ) : '';
	}
	
	private function _setVar( $match )
	{
		return isset( $this->data_var[ $match[ 1 ] ] ) ? $this->data_var[ $match[ 1 ] ] : '';
	}
	
	private function _setBlock( $match )
	{
		return $this->blk( $this->tpl_result, $this->data_blk, $match[ 1 ], '', false );
	}
	
	public function compile($file)
	{
		$this->tpl_result = file_get_contents( $file );
		if ( empty( $this->tpl_result ) )
		{
			trigger_error( 'Theme::compile(): El archivo esta vacio.', E_USER_ERROR );
		}
		
		$this->tpl_result = preg_replace_callback( "#<!--\s+INCLUDE\s+(.+)\s+-->#sU", array($this, "_includeTpl"), $this->tpl_result );
		
		if( ! empty( $this->data_blk ) )
		{
			$this->tpl_result = preg_replace_callback( "#<!--\s+BEGIN\s+([0-9A-Za-z_-]+)\s+-->(.*?)<!--\s+END\s+\\1\s+-->#s", array($this, "_setBlock"), $this->tpl_result );
		}
		
		if( ! empty( $this->datos_var ) )
		{
			$this->tpl_result = preg_replace_callback( "#{([a-zA-Z0-9\_\-\+\./]+)\}#", array($this, "_setVar"), $this->tpl_result );
		}
		
		$this->tpl_result = preg_replace( "#<!--\s+BEGIN\s+([0-9A-Za-z_-]+)\s+-->(.*?)<!--\s+END\s+\\1\s+-->#s", "", $this->tpl_result );
		
		return $this->tpl_result;
	}
	
	private function blk( $text, $object, $block, $orig_block = '', $sub = false )
	{
		if( empty( $block ) )
		{
			return $text;
		}
		
		$text_blk = '';
		
		if( preg_match( "#<!--\s+BEGIN\s+" . $block . "\s+-->(.*?)<!-- END\s+" . $block . "\s+-->#s", $text_result, $text ) )
		{
			$text_blk = $text_result[1];
		}
		
		$orig_block = ($sub === true) ? $orig_block . "." : "";
		
		$final_block = array();
		
		$final_block[$block] = "";
		
		$contar = !isset( $object[$block] ) ? -1 : sizeof($object[$block]) - 1;
		
		for($i = 0; $i <= $count; $i++)
		{
			$orig_blk = $repl_blk = $sublock = array();
			
			foreach( $object[$block][$i] as $var => $value )
			{
				if( !empty( $var ) && is_array( $value ) && !in_array( $var, $sublock ) )
				{
					$sublock[] = $var;
				}
				else
				{
					$orig_blk[] = "/{" . $orig_blocke . $blocke . "." . $var . "}/";
					$repl_blk[] = $value;
				}
			}
			
			$texto_proc = $texto_blk;
			
			$count2 = sizeof($sublock) - 1;
			for($m = 0; $m <= $count2; $m++)
			{
				$text_proc = $this->blk( $text_proc, $object[$block][$i], $sublock[$m], $block, true );
			}
			
			$final_block[$block] .= preg_replace( $orig_blk, $repl_blk, ' ' . $text_proc . ' ' );
		}
		
		$text = ($sub === true) ? preg_replace( "#<!--\s+BEGIN\s+" . $block . "\s+-->(.*?)<!--\s+END\s+" . $block . "\s+-->#s", $final_block[$block], $text ) : $final_block[$block];
		
		return $text;
	}
}

?>