Skip to content

Commit

Permalink
[Templating] refactored helpers, added helper slots, made some speed …
Browse files Browse the repository at this point in the history
…optimizations
  • Loading branch information
fabpot committed Feb 5, 2010
1 parent ec2a3bc commit da36417
Show file tree
Hide file tree
Showing 8 changed files with 220 additions and 245 deletions.
178 changes: 57 additions & 121 deletions src/Symfony/Components/Templating/Engine.php
Expand Up @@ -3,7 +3,6 @@
namespace Symfony\Components\Templating;

use Symfony\Components\Templating\Loader\LoaderInterface;
use Symfony\Components\Templating\Helper\HelperSet;
use Symfony\Components\Templating\Renderer\PhpRenderer;
use Symfony\Components\Templating\Renderer\RendererInterface;

Expand All @@ -28,31 +27,29 @@ class Engine
protected $loader;
protected $renderers;
protected $current;
protected $helperSet;
protected $helpers;
protected $parents;
protected $stack;
protected $slots;
protected $openSlots;
protected $charset;

/**
* Constructor.
*
* @param LoaderInterface $loader A loader instance
* @param array $renderers An array of renderer instances
* @param HelperSet $helperSet A helper set instance
* @param array $helpers A array of helper instances
*/
public function __construct(LoaderInterface $loader, array $renderers = array(), HelperSet $helperSet = null)
public function __construct(LoaderInterface $loader, array $renderers = array(), array $helpers = array())
{
$this->loader = $loader;
$this->renderers = $renderers;
$this->helpers = array();
$this->parents = array();
$this->stack = array();
$this->slots = array();
$this->openSlots = array();
$this->charset = 'UTF-8';
$this->cache = array();

$this->setHelperSet(null === $helperSet ? new HelperSet() : $helperSet);
$this->addHelpers($helpers);

if (!isset($this->renderers['php']))
{
Expand Down Expand Up @@ -84,14 +81,23 @@ public function __construct(LoaderInterface $loader, array $renderers = array(),
*/
public function render($name, array $parameters = array())
{
list($name, $options) = $this->splitTemplateName($name);
if (isset($this->cache[$name]))
{
list($name, $options, $template) = $this->cache[$name];
}
else
{
list($name, $options) = $this->splitTemplateName($old = $name);

// load
$template = $this->loader->load($name, $options);
// load
$template = $this->loader->load($name, $options);

if (false === $template)
{
throw new \InvalidArgumentException(sprintf('The template "%s" does not exist (renderer: %s).', $name, $options['renderer']));
if (false === $template)
{
throw new \InvalidArgumentException(sprintf('The template "%s" does not exist (renderer: %s).', $name, $options['renderer']));
}

$this->cache[$old] = array($name, $options, $template);
}

$this->current = $name;
Expand All @@ -114,40 +120,18 @@ public function render($name, array $parameters = array())
// decorator
if ($this->parents[$name])
{
$this->stack[] = $this->get('content');
$this->set('content', $content);
$slots = $this->get('slots');
$this->stack[] = $slots->get('content');
$slots->set('content', $content);

$content = $this->render($this->parents[$name], $parameters);

$this->set('content', array_pop($this->stack));
$slots->set('content', array_pop($this->stack));
}

return $content;
}

/**
* Sets a helper value.
*
* @param string $name The helper name
* @param HelperInterface $value The helper value
*/
public function setHelperSet(HelperSet $helperSet)
{
$this->helperSet = $helperSet;

$helperSet->setEngine($this);
}

/**
* Gets all helper values.
*
* @return array An array of all helper values
*/
public function getHelperSet()
{
return $this->helperSet;
}

/**
* Gets a helper value.
*
Expand All @@ -159,121 +143,73 @@ public function getHelperSet()
*/
public function __get($name)
{
return $this->$name = $this->helperSet->get($name);
return $this->$name = $this->get($name);
}

/**
* Decorates the current template with another one.
*
* @param string $template The decorator logical name
*/
public function extend($template)
public function addHelpers(array $helpers = array())
{
$this->parents[$this->current] = $template;
}

/**
* Starts a new slot.
*
* This method starts an output buffer that will be
* closed when the stop() method is called.
*
* @param string $name The slot name
*
* @throws \InvalidArgumentException if a slot with the same name is already started
*/
public function start($name)
{
if (in_array($name, $this->openSlots))
foreach ($helpers as $alias => $helper)
{
throw new \InvalidArgumentException(sprintf('A slot named "%s" is already started.', $name));
$this->set($helper, is_int($alias) ? null : $alias);
}

$this->openSlots[] = $name;
$this->slots[$name] = '';

ob_start();
ob_implicit_flush(0);
}

/**
* Stops a slot.
* Sets a helper.
*
* @throws \LogicException if no slot has been started
* @param HelperInterface $value The helper instance
* @param string $alias An alias
*/
public function stop()
public function set(HelperInterface $helper, $alias = null)
{
$content = ob_get_clean();

if (!$this->openSlots)
$this->helpers[$helper->getName()] = $helper;
if (null !== $alias)
{
throw new \LogicException('No slot started.');
$this->helpers[$alias] = $helper;
}

$name = array_pop($this->openSlots);

$this->slots[$name] = $content;
$helper->setEngine($this);
}

/**
* Returns true if the slot exists.
* Returns true if the helper if defined.
*
* @param string $name The helper name
*
* @param string $name The slot name
* @return Boolean true if the helper is defined, false otherwise
*/
public function has($name)
{
return isset($this->slots[$name]);
return isset($this->helpers[$name]);
}

/**
* Gets the slot value.
* Gets a helper value.
*
* @param string $name The slot name
* @param string $default The default slot content
* @param string $name The helper name
*
* @return string The slot content
*/
public function get($name, $default = false)
{
return isset($this->slots[$name]) ? $this->slots[$name] : $default;
}

/**
* Sets a slot value.
* @return HelperInterface The helper instance
*
* @param string $name The slot name
* @param string $content The slot content
* @throws \InvalidArgumentException if the helper is not defined
*/
public function set($name, $content)
public function get($name)
{
$this->slots[$name] = $content;
if (!isset($this->helpers[$name]))
{
throw new \InvalidArgumentException(sprintf('The helper "%s" is not defined.', $name));
}

return $this->helpers[$name];
}

/**
* Outputs a slot.
*
* @param string $name The slot name
* @param string $default The default slot content
* Decorates the current template with another one.
*
* @return Boolean true if the slot is defined or if a default content has been provided, false otherwise
* @param string $template The decorator logical name
*/
public function output($name, $default = false)
public function extend($template)
{
if (!isset($this->slots[$name]))
{
if (false !== $default)
{
echo $default;

return true;
}

return false;
}

echo $this->slots[$name];

return true;
$this->parents[$this->current] = $template;
}

/**
Expand Down
20 changes: 11 additions & 9 deletions src/Symfony/Components/Templating/Helper/Helper.php
Expand Up @@ -2,6 +2,8 @@

namespace Symfony\Components\Templating\Helper;

use Symfony\Components\Templating\Engine;

/*
* This file is part of the symfony package.
*
Expand All @@ -20,25 +22,25 @@
*/
abstract class Helper implements HelperInterface
{
protected $helperSet;
protected $engine;

/**
* Sets the helper set associated with this helper.
* Sets the engine associated with this helper.
*
* @param HelperSet $helperSet A HelperSet instance
* @param Engine $engine A Engine instance
*/
public function setHelperSet(HelperSet $helperSet = null)
public function setEngine(Engine $engine = null)
{
$this->helperSet = $helperSet;
$this->engine = $engine;
}

/**
* Gets the helper set associated with this helper.
* Gets the engine associated with this helper.
*
* @return HelperSet A HelperSet instance
* @return Engine A Engine instance
*/
public function getHelperSet()
public function getEngine()
{
return $this->helperSet;
return $this->engine;
}
}
14 changes: 8 additions & 6 deletions src/Symfony/Components/Templating/Helper/HelperInterface.php
Expand Up @@ -2,6 +2,8 @@

namespace Symfony\Components\Templating\Helper;

use Symfony\Components\Templating\Engine;

/*
* This file is part of the symfony package.
*
Expand All @@ -28,16 +30,16 @@ interface HelperInterface
function getName();

/**
* Sets the helper set associated with this helper.
* Sets the engine associated with this helper.
*
* @param HelperSet $helperSet A HelperSet instance
* @param Engine $engine A Engine instance
*/
function setHelperSet(HelperSet $helperSet = null);
function setEngine(Engine $engine = null);

/**
* Gets the helper set associated with this helper.
* Gets the engine associated with this helper.
*
* @return HelperSet A HelperSet instance
* @return Engine A Engine instance
*/
function getHelperSet();
function getEngine();
}

0 comments on commit da36417

Please sign in to comment.