Location: PHPKode > projects > Shinobu > system/lib/template_parser.php
<?php

/* ---

	Copyright (C) 2008-2009 Frank Smit
	http://shinobu.61924.nl/

	This file is part of Shinobu.

	Shinobu 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 3 of the License, or
	(at your option) any later version.

	Shinobu 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.

	You should have received a copy of the GNU General Public License
	along with Shinobu. If not, see <http://www.gnu.org/licenses/>.

--- */

class template_parser
{
	public $template, $template_path;
	private $tags, $tag_data, $addon_tags = array('prefix'=>array(),'suffix'=>array());

	public function __construct($template_path)
	{
		$this->template_path = $template_path;

		if (!is_file($template_path))
			error('Can\'t find template file!', __FILE__, __LINE__);

		$this->template = file_get_contents($template_path);
	}

	public function assign($tag_names, $data)
	{
		if (is_array($tag_names))
		{
			$tag_count = count($tag_names);

			if(count($data) == $tag_count)
			for ($x = 0;$x < $tag_count;$x++)
			{
				$this->tags[$tag_names[$x]] = '[tpl:'.$tag_names[$x].']';
				$this->tag_data[$tag_names[$x]] = $data[$x];
			}
		}
		else
		{
			$this->tags[$tag_names] = '[tpl:'.$tag_names.']'; // Add tag to tag array
			$this->tag_data[$tag_names] = $data; // Add data to data array
		}
	}

	public function add($tag_name, $prefix='', $suffix='')
	{
		if ($this->tag_exists($tag_name))
		{
			if (!empty($prefix))
				$this->addon_tags['prefix'][$tag_name][] = $prefix;

			if (!empty($suffix))
				$this->addon_tags['suffix'][$tag_name][] = $suffix;
		}

	}

	public function assigned($tag_name)
	{
		return array_key_exists($tag_name, $this->tags);
	}

	public function tag_exists($tage_name)
	{
		return preg_match('/\[tpl\:'.$tage_name.'\]/', $this->template);
	}

	public function process()
	{
		// Merge data from the addon tags with the data from the main tags
		if (count($this->addon_tags['prefix']) > 0)
			foreach ($this->addon_tags['prefix'] as $key => $value)
			{
				$value = array_reverse($value); // <- Get the addon tags in the right order
				if ($this->assigned($key))
					foreach ($value as $v)
						$this->tag_data[$key] = $v.$this->tag_data[$key];
			}

		if (count($this->addon_tags['suffix']) > 0)
			foreach ($this->addon_tags['suffix'] as $key => $value)
			{
				if ($this->assigned($key))
					foreach ($value as $v)
						$this->tag_data[$key] .= $v;
			}

		if (COMPILE_TEMPLATES === true)
		{
			$md5 = md5($this->template_path).'.tpl.php';

			if (!file_exists(SYS_CACHE_DIR.$md5))
			{
				if (strpos($this->template, '<?xml') !== false)
					$this->template = str_replace('<?xml', '<?php echo \'<?xml\' ?>', $this->template);

				$compiled_template = preg_replace('/\[tpl\:(.*?)\]/', '<?php echo \$this->tag_data[\'$1\']; ?>', $this->template);
				cache_data($md5, $compiled_template);
			}

			ob_start();
			require SYS_CACHE_DIR.$md5;
			$this->template = ob_get_clean();
		}
		else
		{
			// Replace tags with tag data
			$this->template = str_replace($this->tags, $this->tag_data, $this->template);
		}
	}

	public function output()
	{
		return $this->template;
	}
}

?>
Return current item: Shinobu