Location: PHPKode > scripts > UI Template > ui-template/frea/Template/CodeBuilder.php
<?php
/**
 * Build template from ui dom
 *
 * @package frea-framework
 * @subpackage Template
 * 
 * @copyright 2009 frea-framework
 * @author Dawid Kraczkowski hide@address.com
 * @license  http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
 */
class Template_CodeBuilder
{
	/**
	 * template dom
	 *
	 * @var Template_Dom_Node_Document
	 */
	private $dom;
	/**
	 * filename
	 *
	 * @var string
	 */
	private $filename;

	private $functionname;
	private $preprocessor;
	
	/**
	 * constructor
	 *
	 * @param Template_Dom_Node_Document $document
	 * @param string $filename
	 */
	public function __construct(Template_Dom_Node_Document $document,$filename)
	{
		
		$this->dom = $document;
		$this->filename = $filename;

	}
	/**
	 * parse ui dom children to a
	 * php template code
	 *
	 * @param array $children
	 * @return string
	 */
	public function parseChildren($children)
	{
		$str = "";
		$tmp = "";
		foreach((array)$children as $child)
		{
			if(!$child->disabled) #if is not disabled return parsed contents
			{
				//handle text nodes
				if($child->nodeType === Template_Dom_Node::TEXT_NODE)
				{
					$tmp = "";
					$tmp = $child->value();
					$child->parsedContents = $tmp;
					
					$str .= $tmp;
				}
				//hanlde children
				elseif($child->hasChildNodes())
				{
					$tmp = "";
					if(!empty($child->namespacetag))$tmp .= $child->namespacetag->insertBefore();
					$tmp .= $this->parseChildren($child->children());
					if(!empty($child->namespacetag))$tmp .= $child->namespacetag->insertAfter();
					
					$child->parsedContents = $tmp;
					if(!empty($child->namespacetag))$this->preprocessor[] = $child->namespacetag->preprocessor();
					$str .= $tmp;
				}
				//handle empty tag
				else 
				{
					$tmp = "";	
					if(!empty($child->namespacetag))$tmp .= $child->namespacetag->insertBefore();
					if(!empty($child->namespacetag))$tmp .= $child->namespacetag->insertAfter();
					
					$child->parsedContents = $tmp;
					if(!empty($child->namespacetag))$this->preprocessor[] = $child->namespacetag->preprocessor();
					$str .= $tmp;
				}
			}
			else #handle disables children
			{
				if($child->nodeType === Template_Dom_Node::TEXT_NODE)
				{
					$tmp = $child->value();
					$child->parsedContents = $tmp;
					
				}
				elseif($child->hasChildNodes())
				{
					$tmp = "";
					if(!empty($child->namespacetag))$tmp .= $child->namespacetag->insertBefore();
					$tmp .= $this->parseChildren($child->children());
					if(!empty($child->namespacetag))$tmp .= $child->namespacetag->insertAfter();
					
					
					$child->parsedContents = $tmp;
					if(!empty($child->namespacetag))$this->preprocessor[] = $child->namespacetag->preprocessor();

				}
				else 
				{
					$tmp = "";	
					if(!empty($child->namespacetag))$tmp .= $child->namespacetag->insertBefore();
					if(!empty($child->namespacetag))$tmp .= $child->namespacetag->insertAfter();
					
					$child->parsedContents = $tmp;
					if(!empty($child->namespacetag))$this->preprocessor[] = $child->namespacetag->preprocessor();

				}
			}
			
		}
		return $str;
	}
	/**
	 * build template file and save it
	 * to cache file
	 *
	 * @return string
	 */
	public function build()
	{
		$contents = $this->parseChildren($this->dom->children());
		
		$built = "<?php \n#preprocessor stuff \n"; 
		foreach ((array)$this->preprocessor as $item) 
		{
			$built .= $item;	
		}
		$built .="
#------------------------------------------------template contents------------------------------------------------ \n?> \n";
		$built .= $contents;
		
		$built .= "<?php
#----------------------------------------------end template contents----------------------------------------------
#----------------------------------------------file built with UI Template \n?>";
		file_put_contents($this->filename,$built);
		
		return $built;
	}
	
}
?>
Return current item: UI Template