123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296 |
- <?php
- namespace Symfony\Components\Templating;
- use Symfony\Components\Templating\Loader\LoaderInterface;
- use Symfony\Components\Templating\Renderer\PhpRenderer;
- use Symfony\Components\Templating\Renderer\RendererInterface;
- /*
- * 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
- {
- protected $loader;
- protected $renderers;
- protected $current;
- protected $helpers;
- protected $parents;
- protected $stack;
- protected $charset;
- /**
- * Constructor.
- *
- * @param LoaderInterface $loader A loader instance
- * @param array $renderers An array of renderer instances
- * @param array $helpers A array of helper instances
- */
- 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->charset = 'UTF-8';
- $this->cache = array();
- $this->addHelpers($helpers);
- if (!isset($this->renderers['php']))
- {
- $this->renderers['php'] = new PhpRenderer();
- }
- foreach ($this->renderers as $renderer)
- {
- $renderer->setEngine($this);
- }
- }
- /**
- * Renders a template.
- *
- * 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
- * @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())
- {
- 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);
- 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;
- $this->parents[$name] = null;
- // renderer
- $renderer = $template->getRenderer() ? $template->getRenderer() : $options['renderer'];
- if (!isset($this->renderers[$options['renderer']]))
- {
- 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])
- {
- $slots = $this->get('slots');
- $this->stack[] = $slots->get('_content');
- $slots->set('_content', $content);
- $content = $this->render($this->parents[$name], $parameters);
- $slots->set('_content', array_pop($this->stack));
- }
- return $content;
- }
- /**
- * Outputs a rendered template.
- *
- * @param string $name A template name
- * @param array $parameters An array of parameters to pass to the template
- *
- * @see render()
- */
- public function output($name, array $parameters = array())
- {
- echo $this->render($name, $parameters);
- }
- /**
- * Gets a helper value.
- *
- * @param string $name The helper name
- *
- * @return mixed The helper value
- *
- * @throws \InvalidArgumentException if the helper is not defined
- */
- public function __get($name)
- {
- return $this->$name = $this->get($name);
- }
- public function addHelpers(array $helpers = array())
- {
- foreach ($helpers as $alias => $helper)
- {
- $this->set($helper, is_int($alias) ? null : $alias);
- }
- }
- /**
- * Sets a helper.
- *
- * @param HelperInterface $value The helper instance
- * @param string $alias An alias
- */
- public function set(HelperInterface $helper, $alias = null)
- {
- $this->helpers[$helper->getName()] = $helper;
- if (null !== $alias)
- {
- $this->helpers[$alias] = $helper;
- }
- $helper->setEngine($this);
- }
- /**
- * Returns true if the helper if defined.
- *
- * @param string $name The helper name
- *
- * @return Boolean true if the helper is defined, false otherwise
- */
- public function has($name)
- {
- return isset($this->helpers[$name]);
- }
- /**
- * Gets a helper value.
- *
- * @param string $name The helper name
- *
- * @return HelperInterface The helper instance
- *
- * @throws \InvalidArgumentException if the helper is not defined
- */
- public function get($name)
- {
- if (!isset($this->helpers[$name]))
- {
- throw new \InvalidArgumentException(sprintf('The helper "%s" is not defined.', $name));
- }
- return $this->helpers[$name];
- }
- /**
- * Decorates the current template with another one.
- *
- * @param string $template The decorator logical name
- */
- public function extend($template)
- {
- $this->parents[$this->current] = $template;
- }
- /**
- * 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;
- }
- /**
- * Gets the loader associated with this engine.
- *
- * @return LoaderInterface A LoaderInterface instance
- */
- public function getLoader()
- {
- return $this->loader;
- }
- /**
- * 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);
- }
- 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));
- }
- }
|