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

/**
 * Native PHP Template Renderer for Claw Framework Library
 *
 * @package claw.ext
 * @author Tomas Varaneckas <tomas [dot] varaneckas [at] gmail [dot] com>
 * @version $Id: PHPClawRendererLib.php 120 2006-03-19 19:26:44Z spajus $
 * @todo implementation + testing
 * @todo FIXME - broken
 */
class PHPClawRendererLib
{
	/**
	 * Configuration, passed from PHPClawRenderer
	 *
	 * @var Array
	 */
	private $config = array();

	/**
	 * Template parts, passed from PHPClawRenderer
	 *
	 * @var Array
	 */
	private $parts = array();

	/**
	 * Constructor
	 *
	 * @param Array $config Configuration
	 * @param Array $parts Template parts
	 * @param Array $vars Variables
	 */
	public function __construct(&$config, &$parts, &$vars)
	{
		$this->config   =& $config;
		$this->parts	=& $parts;
		$this->vars	 =& $vars;
	}

	/**
	 * Displays template part
	 *
	 * @param string $name Part name
	 * @param bool $optional Throw exception if not optional and not set?
	 * @param bool $condition Will not display if false
	 * @param Array $vars Optional array of additional variables to pass
	 */
	public function part($name, $optional = false, $condition = true, $vars = array())
	{
		if ($condition)
		{
			$tpl = $this;
			extract($this->vars, EXTR_REFS);
			if (!$optional && !array_key_exists($name, $this->parts))
			{
				throw new ClawException('template part not set: ' . $name);
			}
			if (!$optional && array_key_exists($name, $this->parts)
				&& !file_exists($this->parts[$name]))
			{
				throw new ClawException('template part does not exist: '
					. $name . '(' . $this->parts[$name] . ')');
			}
			if (count($vars))
			{
				extract($vars, EXTR_REFS);
			}
			@include_once $this->parts[$name];
		}
	}

	/**
	 * Gets link
	 *
	 * @param string $to Destination page
	 * @param bool $clean Clear variables?
	 * @param string $method Destination method
	 * @param int $num_var Numeric variable of destination page/method
	 * @param Array $params Array of page/method variables
	 */
	public function link($to = 'index', $clean = false, $method = false
		, $num_var = null, $params = array())
	{
		$claw = Claw::getClaw();
		$chain = $claw->getChain();
		$found = false;
		while (true)
		{
			if ($chain->getTitle() == $to)
			{
				$link = $chain->getPageLink($method, null
					, $clean);
				$found = true;
				break;
			}
			if ($chain->getParent())
			{
				$chain = $chain->getParent();
			}
			else
			{
				break;
			}
		}

		if (!$found)
		{
			$link .= $chain->getPageLink(null, null, $clean)
			      . '/' . $to;
		}

		if ($num_var || count($params))
		{
			$args = new ClawArgs($params);
			$args->setNumVar($num_var);
			$link = trim($link, '/') . '/' . $args->serialize();
		}

		echo trim($claw->getAppURL() . '/' . trim($link, '/'), '/') . '/';
	}

	/**
	 * Get some configuration variable
	 */
	public function cfg()
	{
		echo Claw::getClaw()->getConfig()->get(func_get_args());
	}

	/**
	 * Gets path to template directory
	 */
	public function dir()
	{
		echo Claw::getClaw()->getAppURL(true)
			. '/' . trim($this->config['template_dir'], '/');
	}

	/**
	 * Inserts script tag
	 *
	 * @param string $type Script type
	 * @param string $content Content
	 */
	public function script($type = 'text/javascript', $content)
	{
		if ($content)
		{
			echo "<script type=\"{$type}\">$content</script>";
		}
	}
}
?>
Return current item: Claw