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

/**
 * Smarty Template Engine Renderer for Claw Framework Library
 *
 * @package claw.ext
 * @author Tomas Varaneckas <tomas [dot] varaneckas [at] gmail [dot] com>
 * @version $Id: SmartyClawRendererLib.php 143 2006-04-06 13:43:13Z metasite $
 */
class SmartyClawRendererLib extends SmartyClawRendererHelper
{
	/**
	 * Array of block function names
	 *
	 * @var Array
	 */
	public $blocks = array('script');

	/**
	 * Gets array of block functions
	 *
	 * @return Array
	 */	
	public function getBlockFunctions()
	{
		return $this->blocks;
	}

	/**
	 * Gets array of allowed functions
	 *
	 * @return Array
	 */		
	public function getAllowedFunctions()
	{
		return array();
	}

	/**
	 * Use smarty tags?
	 *
	 * @return boolean
	 */
	public function useSmartyTags()
	{
		return true;
	}	

	/**
	 * Loads template part
	 * Usage: {tpl->part name='part name'
	 *           [condition=true|false] [optional=true|false]}
	 * Params:
	 *      string  'name'      template part name
	 *      bool    'optional'  default false,
	 *           if true - no errors if template does not exist
	 *      bool    'condition' condition for
	 *           template part to appear (default: true)
	 *
	 * @param Array $params Array: 'name', 'optional'
	 *       (optional, default = false)
	 * @param Smarty $smarty Smarty Template Engine
	 * @return string
	 */
	public function part($params, &$smarty)
	{
		$condition = empty($params['condition'])
		             ? true : $params['condition'];

		$optional = empty($params['optional'])
		            ? false : $params['optional'];
		if ($condition)
		{
			$tpl_file = $smarty->get_template_vars('__tpl_part_'.$params['name']);
			$ok = $smarty->template_exists($tpl_file);
			if (!$optional && !$ok)
			{
				throw new ClawException('Template part not available: '
					. $params['name'] . ' (' . $tpl_file . ')');
			}
			if ($ok)
			{
				return $smarty->fetch($tpl_file);
			}
		}
	}

	/**
	 * Gets link to chain element
	 * Usage:
	 *      {tpl->link}
	 *      {tpl->link to='pagetitle' [clean=true|false] [rewrite_vars=true|false]
	 *       [num_var=123] [[var1=value1][abc=xyz..]]}
	 * Params:
	 *      string  'to'        target ClawPage or method
	 * 		string  'method'	target method if 'to' was ClawPage
	 *      bool    'rewrite_vars'   if true, new variables will eliminate old 
	 *                               ones with same keys. default = false
	 *      bool    'clean'     if previous variables should be wiped from link
	 *      float   'num_var'   numeric variable to be attached at the end
	 *      mixed   any other   variables to be attached at the end
	 *
	 * @param Array $params Array of Smarty template params
	 * @return string
	 */
	public function link($params)
	{
		$claw = Claw::getClaw();
		$chain = $claw->getChain();
		$to = !empty($params['to']) ? $params['to'] : 'index';
		$clean = empty($params['clean']) ? false : $params['clean'];
		$found = false;
		while (true)
		{
			if ($chain->getTitle() == $to)
			{
				$params['method'] = ($to == 'index' && empty($params['method']))
					? false : $params['method'];
				$link = $chain->getPageLink($params['method'], null
					, $clean);
				$found = true;
				break;
			}
			if ($chain->getParent())
			{
				$chain = $chain->getParent();
			}
			else
			{
				break;
			}
		}

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

		$num_var = empty($params['num_var']) ? null : $params['num_var'];
		$rewrite_vars = empty($params['rewrite_vars']) 
			? true : $params['rewrite_vars'];
		unset($params['num_var'], $params['to'], $params['clean']
			, $params['method'], $params['rewrite_vars']);

		if ($num_var || count($params))
		{
			$args = new ClawArgs($params);
			$args->setNumVar($num_var);
			if ($rewrite_vars)
			{
				$keys = array_keys($params);
				$keys = implode('|', $keys);
				$link = preg_replace("/\/({$keys})\.[^\/]*\/?/i", "/", $link);
			}
			$link = trim($link, '/') . '/' . $args->serialize();
		}

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

	/**
	 * Configuration variable getter
	 * Usage:
	 *      {tpl->cfg get='var'}
	 *      {tpl->cfg get='var1' get2='var2' ..}
	 *
	 * @param Array $params Array of Smarty template params
	 * @return mixed
	 */
	public function cfg($params)
	{
		return Claw::getClaw()->getConfig()->get(array_values($params));
	}

	/**
	 * Gets path to Smarty template directory
	 *
	 * @return string
	 */
	public function dir($params, &$smarty)
	{
		return Claw::getClaw()->getAppURL(true)
			. '/' . trim($smarty->template_dir, '/');
	}

	/**
	 * Wraps content into <script></script> tag.
	 * Block function.
	 *
	 * Usage:
	 *      {tpl->script}
	 *       {ajax->create_updater ...wrap=false}
	 *       {ajax->repeat ...}
	 *      {/tpl->script}
	 *
	 * Params:
	 *      string  'type'  script type param. default = text/javascript
	 * @return string
	 */
	public function script($params, $content)
	{
		if ($content)
		{
			$type = $params['type'] ? $params['type'] : 'text/javascript';
			return "<script type=\"{$type}\">$content</script>";
		}
	}
}
?>
Return current item: Claw