Location: PHPKode > scripts > Claw > claw/ext/SmartyClawRenderer.php
<?php

/**
 * Smarty Template Engine Renderer for Claw Framework
 *
 * @author Tomas Varaneckas <tomas [dot] varaneckas [at] gmail [dot] com>
 * @package claw.ext
 * @version $Id: SmartyClawRenderer.php 120 2006-03-19 19:26:44Z spajus $
 */
class SmartyClawRenderer implements ClawRenderer
{
	/**
	 * Smarty template engine
	 *
	 * @var Smarty
	 */
	private $smarty;

	/**
	 * Current template filename
	 *
	 * @var string
	 */
	private $template;

	/**
	 * Constructor. Loads Smarty, configures it and
	 * adds custom template object {tpl}
	 *
	 * @see SmartyClawRendererLib
	 */
	public function __construct()
	{
		$cfg = Claw::getClaw()->getConfig();
		$smarty_cfg = $cfg->getArray('claw_renderer', 'config');

		if (!@include_once($smarty_cfg['include_file']))
		{
			throw new ClawException('Cannot load Smarty renderer: '
				. $smarty_cfg['include_file']);
		}
		$this->smarty = new Smarty();

		unset($smarty_cfg['include_file']);
		foreach ($smarty_cfg as $key => $value)
		{
			$this->smarty->$key = $value;
		}

		$smarty_claw = new SmartyClawRendererLib();
		$this->addHelper('tpl', $smarty_claw);

	}

	/**
	 * Registers Smarty object
	 *
	 * @param string $title Object title
	 * @param Object $object
	 * @param null|array $allowed Allowed methods
	 * @param array $block_methods Array of block method titles
	 */
	protected function registerObject($title, $object,
		$allowed = null, $use_smarty_tags, $block_methods = array())
	{
		$this->smarty->register_object(
			$title, $object, $allowed, $use_smarty_tags, $block_methods);
	}

	/**
	 * Assigns array of variables to template engine
	 *
	 * @param Array $vars Array of variables to assign
	 */
	public function assignVars($vars)
	{
		if (is_array($vars))
		{
			$this->smarty->assign($vars);
		}
	}

	/**
	 * Assigns variable to template engine
	 *
	 * @param string $var Name
	 * @param mixed $value Value
	 */
	public function assign($var, $value)
	{
		$this->smarty->assign($var, $value);
	}

	/**
	 * Sets main template file
	 *
	 * @param string $tpl Path to template file
	 */
	public function setTemplate($tpl)
	{
		$this->template = $tpl;
	}

	/**
	 * Sets inclusive template part
	 *
	 * @param string $name Name of template part
	 * @param string $tpl Path to template part file
	 */
	public function setTemplatePart($name, $tpl)
	{
		$this->smarty->assign('__tpl_part_' . $name, $tpl);
	}

	/**
	 * Adds ClawRendererHelper object for use within templates
	 * Syntax and the rest depends on implementation
	 *
	 * @param string $name Helper name 
	 * @param ClawRendererHelper $helper Helper Object
	 */
	public function addHelper($name, ClawRendererHelper $helper)	
	{
		if (!($helper instanceof SmartyClawRendererHelper))
		{
			throw new ClawException("Invalid SmartyClawRendererHelper: " 
				. get_class($helper));
		}
		$helper->setName($name);
		$this->registerObject($name, $helper, $helper->getAllowedFunctions()
			, $helper->useSmartyTags(), $helper->getBlockFunctions());
	}

	/**
	 * Returns rendered output
	 *
	 * @return string
	 */
	public function render()
	{
		if ($this->template)
		{
			return $this->smarty->fetch($this->template);
		}
	}
}
?>
Return current item: Claw