|
@@ -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;
|
|
|
|
|
@@ -28,11 +27,9 @@ class Engine
|
|
|
protected $loader;
|
|
|
protected $renderers;
|
|
|
protected $current;
|
|
|
- protected $helperSet;
|
|
|
+ protected $helpers;
|
|
|
protected $parents;
|
|
|
protected $stack;
|
|
|
- protected $slots;
|
|
|
- protected $openSlots;
|
|
|
protected $charset;
|
|
|
|
|
|
/**
|
|
@@ -40,19 +37,19 @@ class Engine
|
|
|
*
|
|
|
* @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']))
|
|
|
{
|
|
@@ -84,14 +81,23 @@ class Engine
|
|
|
*/
|
|
|
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;
|
|
@@ -114,40 +120,18 @@ class Engine
|
|
|
// 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.
|
|
|
*
|
|
@@ -159,121 +143,73 @@ class Engine
|
|
|
*/
|
|
|
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;
|
|
|
}
|
|
|
|
|
|
/**
|