2010-01-04 14:26:20 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Symfony\Components\Templating;
|
|
|
|
|
|
|
|
use Symfony\Components\Templating\Loader\LoaderInterface;
|
|
|
|
use Symfony\Components\Templating\Helper\HelperSet;
|
|
|
|
use Symfony\Components\Templating\Renderer\PhpRenderer;
|
2010-01-29 08:51:39 +00:00
|
|
|
use Symfony\Components\Templating\Renderer\RendererInterface;
|
2010-01-04 14:26:20 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the symfony package.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Engine is the main class of the templating component.
|
|
|
|
*
|
|
|
|
* @package symfony
|
|
|
|
* @subpackage templating
|
|
|
|
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*/
|
|
|
|
class Engine
|
|
|
|
{
|
2010-01-28 06:04:57 +00:00
|
|
|
protected $loader;
|
|
|
|
protected $renderers;
|
|
|
|
protected $current;
|
|
|
|
protected $helperSet;
|
|
|
|
protected $parents;
|
|
|
|
protected $stack;
|
|
|
|
protected $slots;
|
|
|
|
protected $openSlots;
|
|
|
|
protected $charset;
|
2010-01-04 14:26:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* @param LoaderInterface $loader A loader instance
|
2010-01-29 08:51:39 +00:00
|
|
|
* @param array $renderers An array of renderer instances
|
2010-01-04 14:26:20 +00:00
|
|
|
* @param HelperSet $helperSet A helper set instance
|
|
|
|
*/
|
|
|
|
public function __construct(LoaderInterface $loader, array $renderers = array(), HelperSet $helperSet = null)
|
|
|
|
{
|
|
|
|
$this->loader = $loader;
|
|
|
|
$this->renderers = $renderers;
|
2010-01-28 06:04:57 +00:00
|
|
|
$this->parents = array();
|
|
|
|
$this->stack = array();
|
|
|
|
$this->slots = array();
|
|
|
|
$this->openSlots = array();
|
|
|
|
$this->charset = 'UTF-8';
|
2010-01-04 14:26:20 +00:00
|
|
|
|
|
|
|
$this->setHelperSet(null === $helperSet ? new HelperSet() : $helperSet);
|
|
|
|
|
|
|
|
if (!isset($this->renderers['php']))
|
|
|
|
{
|
|
|
|
$this->renderers['php'] = new PhpRenderer();
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($this->renderers as $renderer)
|
|
|
|
{
|
|
|
|
$renderer->setEngine($this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders a template.
|
|
|
|
*
|
2010-01-28 06:56:34 +00:00
|
|
|
* The template name is composed of segments separated by a colon (:).
|
|
|
|
* By default, this engine knows how to parse templates with one or two segments:
|
|
|
|
*
|
|
|
|
* * index: The template logical name is index and the renderer is php
|
|
|
|
* * index:twig: The template logical name is index and the renderer is twig
|
|
|
|
*
|
|
|
|
* @param string $name A template name
|
2010-01-04 14:26:20 +00:00
|
|
|
* @param array $parameters An array of parameters to pass to the template
|
|
|
|
*
|
|
|
|
* @return string The evaluated template as a string
|
|
|
|
*
|
|
|
|
* @throws \InvalidArgumentException if the renderer does not exist or if the template does not exist
|
|
|
|
* @throws \RuntimeException if the template cannot be rendered
|
|
|
|
*/
|
|
|
|
public function render($name, array $parameters = array())
|
|
|
|
{
|
2010-01-28 06:56:34 +00:00
|
|
|
list($name, $options) = $this->splitTemplateName($name);
|
2010-01-04 14:26:20 +00:00
|
|
|
|
|
|
|
// load
|
2010-01-28 06:56:34 +00:00
|
|
|
$template = $this->loader->load($name, $options);
|
2010-01-04 14:26:20 +00:00
|
|
|
|
|
|
|
if (false === $template)
|
|
|
|
{
|
2010-01-28 06:56:34 +00:00
|
|
|
throw new \InvalidArgumentException(sprintf('The template "%s" does not exist (renderer: %s).', $name, $options['renderer']));
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->current = $name;
|
|
|
|
$this->parents[$name] = null;
|
|
|
|
|
|
|
|
// renderer
|
2010-01-28 06:56:34 +00:00
|
|
|
$renderer = $template->getRenderer() ? $template->getRenderer() : $options['renderer'];
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-01-28 06:56:34 +00:00
|
|
|
if (!isset($this->renderers[$options['renderer']]))
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
|
|
|
throw new \InvalidArgumentException(sprintf('The renderer "%s" is not registered.', $renderer));
|
|
|
|
}
|
|
|
|
|
|
|
|
// render
|
|
|
|
if (false === $content = $this->renderers[$renderer]->evaluate($template, $parameters))
|
|
|
|
{
|
|
|
|
throw new \RuntimeException(sprintf('The template "%s" cannot be rendered (renderer: %s).', $name, $renderer));
|
|
|
|
}
|
|
|
|
|
|
|
|
// decorator
|
|
|
|
if ($this->parents[$name])
|
|
|
|
{
|
|
|
|
$this->stack[] = $this->get('content');
|
|
|
|
$this->set('content', $content);
|
|
|
|
|
|
|
|
$content = $this->render($this->parents[$name], $parameters);
|
|
|
|
|
|
|
|
$this->set('content', array_pop($this->stack));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $content;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets a helper value.
|
|
|
|
*
|
2010-01-29 08:51:39 +00:00
|
|
|
* @param string $name The helper name
|
2010-01-04 14:26:20 +00:00
|
|
|
* @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.
|
|
|
|
*
|
2010-01-29 08:51:39 +00:00
|
|
|
* @param string $name The helper name
|
2010-01-04 14:26:20 +00:00
|
|
|
*
|
|
|
|
* @return mixed The helper value
|
|
|
|
*
|
|
|
|
* @throws \InvalidArgumentException if the helper is not defined
|
|
|
|
*/
|
|
|
|
public function __get($name)
|
|
|
|
{
|
|
|
|
return $this->helperSet->get($name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decorates the current template with another one.
|
|
|
|
*
|
|
|
|
* @param string $template The decorator logical name
|
|
|
|
*/
|
|
|
|
public function extend($template)
|
|
|
|
{
|
|
|
|
$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))
|
|
|
|
{
|
|
|
|
throw new \InvalidArgumentException(sprintf('A slot named "%s" is already started.', $name));
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->openSlots[] = $name;
|
|
|
|
$this->slots[$name] = '';
|
|
|
|
|
|
|
|
ob_start();
|
|
|
|
ob_implicit_flush(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stops a slot.
|
|
|
|
*
|
|
|
|
* @throws \LogicException if no slot has been started
|
|
|
|
*/
|
|
|
|
public function stop()
|
|
|
|
{
|
|
|
|
$content = ob_get_clean();
|
|
|
|
|
|
|
|
if (!$this->openSlots)
|
|
|
|
{
|
|
|
|
throw new \LogicException('No slot started.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$name = array_pop($this->openSlots);
|
|
|
|
|
|
|
|
$this->slots[$name] = $content;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the slot exists.
|
|
|
|
*
|
|
|
|
* @param string $name The slot name
|
|
|
|
*/
|
|
|
|
public function has($name)
|
|
|
|
{
|
|
|
|
return isset($this->slots[$name]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the slot value.
|
|
|
|
*
|
|
|
|
* @param string $name The slot name
|
|
|
|
* @param string $default The default slot content
|
|
|
|
*
|
|
|
|
* @return string The slot content
|
|
|
|
*/
|
|
|
|
public function get($name, $default = false)
|
|
|
|
{
|
|
|
|
return isset($this->slots[$name]) ? $this->slots[$name] : $default;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets a slot value.
|
|
|
|
*
|
|
|
|
* @param string $name The slot name
|
|
|
|
* @param string $content The slot content
|
|
|
|
*/
|
|
|
|
public function set($name, $content)
|
|
|
|
{
|
|
|
|
$this->slots[$name] = $content;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Outputs a slot.
|
|
|
|
*
|
|
|
|
* @param string $name The slot name
|
|
|
|
* @param string $default The default slot content
|
|
|
|
*
|
|
|
|
* @return Boolean true if the slot is defined or if a default content has been provided, false otherwise
|
|
|
|
*/
|
|
|
|
public function output($name, $default = false)
|
|
|
|
{
|
|
|
|
if (!isset($this->slots[$name]))
|
|
|
|
{
|
|
|
|
if (false !== $default)
|
|
|
|
{
|
|
|
|
echo $default;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $this->slots[$name];
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Escapes a string by using the current charset.
|
|
|
|
*
|
|
|
|
* @param string $value A string to escape
|
|
|
|
*
|
|
|
|
* @return string The escaped string or the original value if not a string
|
|
|
|
*/
|
|
|
|
public function escape($value)
|
|
|
|
{
|
|
|
|
return is_string($value) || (is_object($value) && method_exists($value, '__toString')) ? htmlspecialchars($value, ENT_QUOTES, $this->charset) : $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the charset to use.
|
|
|
|
*
|
|
|
|
* @param string $charset The charset
|
|
|
|
*/
|
|
|
|
public function setCharset($charset)
|
|
|
|
{
|
|
|
|
$this->charset = $charset;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the current charset.
|
|
|
|
*
|
|
|
|
* @return string The current charset
|
|
|
|
*/
|
|
|
|
public function getCharset()
|
|
|
|
{
|
|
|
|
return $this->charset;
|
|
|
|
}
|
2010-01-28 06:56:34 +00:00
|
|
|
|
2010-01-29 08:51:39 +00:00
|
|
|
/**
|
|
|
|
* Sets a template renderer.
|
|
|
|
*
|
|
|
|
* @param string $name The renderer name
|
|
|
|
* @param RendererInterface $renderer A RendererInterface instance
|
|
|
|
*/
|
|
|
|
public function setRenderer($name, RendererInterface $renderer)
|
|
|
|
{
|
|
|
|
$this->renderers[$name] = $renderer;
|
|
|
|
$renderer->setEngine($this);
|
|
|
|
}
|
|
|
|
|
2010-01-28 06:56:34 +00:00
|
|
|
protected function splitTemplateName($name)
|
|
|
|
{
|
|
|
|
if (false !== $pos = strpos($name, ':'))
|
|
|
|
{
|
|
|
|
$renderer = substr($name, $pos + 1);
|
|
|
|
$name = substr($name, 0, $pos);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$renderer = 'php';
|
|
|
|
}
|
|
|
|
|
|
|
|
return array($name, array('renderer' => $renderer));
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|