2010-02-17 13:55:05 +00:00
|
|
|
<?php
|
|
|
|
|
2010-07-09 09:05:26 +01:00
|
|
|
namespace Symfony\Bundle\FrameworkBundle\Templating;
|
2010-02-17 13:55:05 +00:00
|
|
|
|
2010-08-20 22:09:55 +01:00
|
|
|
use Symfony\Component\Templating\Engine as BaseEngine;
|
|
|
|
use Symfony\Component\Templating\Loader\LoaderInterface;
|
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface;
|
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
2010-02-17 13:55:05 +00:00
|
|
|
|
|
|
|
/*
|
2010-04-07 01:51:29 +01:00
|
|
|
* This file is part of the Symfony package.
|
2010-02-17 13:55:05 +00:00
|
|
|
*
|
|
|
|
* (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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-11-23 09:22:48 +00:00
|
|
|
* This engine knows how to render Symfony templates.
|
2010-02-17 13:55:05 +00:00
|
|
|
*
|
2010-10-17 12:45:15 +01:00
|
|
|
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
2010-02-17 13:55:05 +00:00
|
|
|
*/
|
|
|
|
class Engine extends BaseEngine
|
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
protected $container;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
2011-01-05 11:13:27 +00:00
|
|
|
* @param ContainerInterface $container The DI container
|
2010-05-06 12:25:53 +01:00
|
|
|
* @param LoaderInterface $loader A loader instance
|
2011-01-05 11:13:27 +00:00
|
|
|
* @param array $renderers All templating renderers
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2011-01-05 11:13:27 +00:00
|
|
|
public function __construct(ContainerInterface $container, LoaderInterface $loader, array $renderers)
|
2010-02-17 13:55:05 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->container = $container;
|
|
|
|
|
2011-01-05 11:13:27 +00:00
|
|
|
parent::__construct($loader, $renderers);
|
2010-02-17 13:55:05 +00:00
|
|
|
}
|
|
|
|
|
2011-01-04 12:59:16 +00:00
|
|
|
public function getContainer()
|
|
|
|
{
|
|
|
|
return $this->container;
|
|
|
|
}
|
|
|
|
|
2010-08-14 18:51:28 +01:00
|
|
|
/**
|
|
|
|
* Renders a view and returns a Response.
|
|
|
|
*
|
|
|
|
* @param string $view The view name
|
|
|
|
* @param array $parameters An array of parameters to pass to the view
|
|
|
|
* @param Response $response A Response instance
|
|
|
|
*
|
|
|
|
* @return Response A Response instance
|
|
|
|
*/
|
|
|
|
public function renderResponse($view, array $parameters = array(), Response $response = null)
|
|
|
|
{
|
|
|
|
if (null === $response) {
|
|
|
|
$response = $this->container->get('response');
|
|
|
|
}
|
|
|
|
|
|
|
|
$response->setContent($this->render($view, $parameters));
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
public function has($name)
|
2010-02-17 13:55:05 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
return isset($this->helpers[$name]);
|
2010-02-17 13:55:05 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* @throws \InvalidArgumentException When the helper is not defined
|
|
|
|
*/
|
|
|
|
public function get($name)
|
2010-02-17 13:55:05 +00:00
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
if (!isset($this->helpers[$name])) {
|
2010-05-06 12:25:53 +01:00
|
|
|
throw new \InvalidArgumentException(sprintf('The helper "%s" is not defined.', $name));
|
|
|
|
}
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if (is_string($this->helpers[$name])) {
|
2010-07-09 13:58:03 +01:00
|
|
|
$this->helpers[$name] = $this->container->get($this->helpers[$name]);
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->helpers[$name]->setCharset($this->charset);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->helpers[$name];
|
2010-02-17 13:55:05 +00:00
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
|
2011-01-05 11:13:27 +00:00
|
|
|
public function setHelpers(array $helpers)
|
|
|
|
{
|
|
|
|
$this->helpers = $helpers;
|
|
|
|
}
|
|
|
|
|
2010-09-28 07:33:33 +01:00
|
|
|
// parses template names following the following pattern:
|
2010-10-04 13:01:33 +01:00
|
|
|
// bundle:section:template(.format).renderer
|
2010-05-27 08:25:16 +01:00
|
|
|
public function splitTemplateName($name, array $defaults = array())
|
2010-02-17 13:55:05 +00:00
|
|
|
{
|
2010-09-28 07:33:33 +01:00
|
|
|
$parts = explode(':', $name);
|
|
|
|
if (3 !== count($parts)) {
|
2010-06-20 19:51:08 +01:00
|
|
|
throw new \InvalidArgumentException(sprintf('Template name "%s" is not valid.', $name));
|
|
|
|
}
|
|
|
|
|
2010-05-27 08:25:16 +01:00
|
|
|
$options = array_replace(
|
|
|
|
array(
|
2010-09-28 07:33:33 +01:00
|
|
|
'format' => '',
|
2010-05-27 08:25:16 +01:00
|
|
|
),
|
|
|
|
$defaults,
|
|
|
|
array(
|
|
|
|
'bundle' => str_replace('\\', '/', $parts[0]),
|
|
|
|
'controller' => $parts[1],
|
|
|
|
)
|
2010-05-06 12:25:53 +01:00
|
|
|
);
|
|
|
|
|
2010-09-28 07:33:33 +01:00
|
|
|
$elements = explode('.', $parts[2]);
|
|
|
|
if (3 === count($elements)) {
|
|
|
|
$parts[2] = $elements[0];
|
2010-10-27 06:18:02 +01:00
|
|
|
if ('html' !== $elements[1]) {
|
|
|
|
$options['format'] = '.'.$elements[1];
|
|
|
|
}
|
2010-09-28 07:33:33 +01:00
|
|
|
$options['renderer'] = $elements[2];
|
|
|
|
} elseif (2 === count($elements)) {
|
|
|
|
$parts[2] = $elements[0];
|
|
|
|
$options['renderer'] = $elements[1];
|
|
|
|
$format = $this->container->get('request')->getRequestFormat();
|
2010-08-17 18:16:36 +01:00
|
|
|
if (null !== $format && 'html' !== $format) {
|
|
|
|
$options['format'] = '.'.$format;
|
|
|
|
}
|
2010-09-28 07:33:33 +01:00
|
|
|
} else {
|
|
|
|
throw new \InvalidArgumentException(sprintf('Template name "%s" is not valid.', $name));
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return array($parts[2], $options);
|
2010-02-17 13:55:05 +00:00
|
|
|
}
|
|
|
|
}
|