2011-01-13 13:58:17 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2011-01-13 13:58:17 +00:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
namespace Symfony\Bundle\TwigBundle;
|
|
|
|
|
2012-04-02 17:28:35 +01:00
|
|
|
use Symfony\Bridge\Twig\TwigEngine as BaseEngine;
|
2011-01-15 13:29:43 +00:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
|
2011-11-07 19:03:35 +00:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference;
|
2011-01-26 13:53:12 +00:00
|
|
|
use Symfony\Component\Templating\TemplateNameParserInterface;
|
2011-01-15 13:29:43 +00:00
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
2011-11-07 19:03:35 +00:00
|
|
|
use Symfony\Component\Config\FileLocatorInterface;
|
2011-01-15 13:29:43 +00:00
|
|
|
|
2011-01-13 13:58:17 +00:00
|
|
|
/**
|
2012-05-16 19:34:56 +01:00
|
|
|
* This engine renders Twig templates.
|
2011-01-13 13:58:17 +00:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
2011-01-13 13:58:17 +00:00
|
|
|
*/
|
2012-04-02 17:28:35 +01:00
|
|
|
class TwigEngine extends BaseEngine implements EngineInterface
|
2011-01-13 13:58:17 +00:00
|
|
|
{
|
2011-11-07 19:03:35 +00:00
|
|
|
protected $locator;
|
2011-01-13 13:58:17 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
2011-01-26 13:53:12 +00:00
|
|
|
* @param \Twig_Environment $environment A \Twig_Environment instance
|
|
|
|
* @param TemplateNameParserInterface $parser A TemplateNameParserInterface instance
|
2012-01-11 14:52:51 +00:00
|
|
|
* @param FileLocatorInterface $locator A FileLocatorInterface instance
|
2011-01-13 13:58:17 +00:00
|
|
|
*/
|
2012-10-13 16:03:39 +01:00
|
|
|
public function __construct(\Twig_Environment $environment, TemplateNameParserInterface $parser, FileLocatorInterface $locator)
|
2011-01-13 13:58:17 +00:00
|
|
|
{
|
2012-04-02 17:28:35 +01:00
|
|
|
parent::__construct($environment, $parser);
|
|
|
|
|
2011-11-07 19:03:35 +00:00
|
|
|
$this->locator = $locator;
|
2011-01-13 13:58:17 +00:00
|
|
|
}
|
|
|
|
|
2012-04-25 11:11:28 +01:00
|
|
|
public function setDefaultEscapingStrategy($strategy)
|
|
|
|
{
|
|
|
|
$this->environment->getExtension('escaper')->setDefaultStrategy($strategy);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function guessDefaultEscapingStrategy($filename)
|
|
|
|
{
|
|
|
|
// remove .twig
|
|
|
|
$filename = substr($filename, 0, -5);
|
|
|
|
|
|
|
|
// get the format
|
|
|
|
$format = substr($filename, strrpos($filename, '.') + 1);
|
|
|
|
|
|
|
|
if ('js' === $format) {
|
|
|
|
return 'js';
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'html';
|
|
|
|
}
|
|
|
|
|
2011-01-13 13:58:17 +00:00
|
|
|
/**
|
|
|
|
* Renders a template.
|
|
|
|
*
|
2011-01-26 13:53:12 +00:00
|
|
|
* @param mixed $name A template name
|
|
|
|
* @param array $parameters An array of parameters to pass to the template
|
2011-01-13 13:58:17 +00:00
|
|
|
*
|
|
|
|
* @return string The evaluated template as a string
|
|
|
|
*
|
|
|
|
* @throws \InvalidArgumentException if the template does not exist
|
|
|
|
* @throws \RuntimeException if the template cannot be rendered
|
|
|
|
*/
|
|
|
|
public function render($name, array $parameters = array())
|
|
|
|
{
|
2011-11-07 19:03:35 +00:00
|
|
|
try {
|
2012-04-02 17:28:35 +01:00
|
|
|
return parent::render($name, $parameters);
|
2011-11-07 20:15:36 +00:00
|
|
|
} catch (\Twig_Error $e) {
|
2011-11-07 19:03:35 +00:00
|
|
|
if ($name instanceof TemplateReference) {
|
|
|
|
try {
|
|
|
|
// try to get the real file name of the template where the error occurred
|
|
|
|
$e->setTemplateFile(sprintf('%s', $this->locator->locate($this->parser->parse($e->getTemplateFile()))));
|
|
|
|
} catch (\Exception $ex) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw $e;
|
|
|
|
}
|
2011-01-13 13:58:17 +00: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) {
|
2011-02-21 16:26:35 +00:00
|
|
|
$response = new Response();
|
2011-01-13 13:58:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$response->setContent($this->render($view, $parameters));
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
}
|