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;
|
|
|
|
|
|
|
|
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
|
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;
|
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface;
|
|
|
|
|
2011-01-13 13:58:17 +00:00
|
|
|
/**
|
|
|
|
* This engine knows how to render Twig templates.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
2011-01-13 13:58:17 +00:00
|
|
|
*/
|
|
|
|
class TwigEngine implements EngineInterface
|
|
|
|
{
|
|
|
|
protected $environment;
|
2011-01-26 13:53:12 +00:00
|
|
|
protected $parser;
|
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
|
|
|
|
* @param GlobalVariables $globals A GlobalVariables instance
|
2011-01-13 13:58:17 +00:00
|
|
|
*/
|
2011-02-21 16:26:35 +00:00
|
|
|
public function __construct(\Twig_Environment $environment, TemplateNameParserInterface $parser, GlobalVariables $globals)
|
2011-01-13 13:58:17 +00:00
|
|
|
{
|
|
|
|
$this->environment = $environment;
|
2011-01-26 13:53:12 +00:00
|
|
|
$this->parser = $parser;
|
2011-01-13 13:58:17 +00:00
|
|
|
|
|
|
|
$environment->addGlobal('app', $globals);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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())
|
|
|
|
{
|
|
|
|
return $this->load($name)->render($parameters);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the template exists.
|
|
|
|
*
|
2011-01-26 13:53:12 +00:00
|
|
|
* @param mixed $name A template name
|
2011-01-13 13:58:17 +00:00
|
|
|
*
|
|
|
|
* @return Boolean true if the template exists, false otherwise
|
|
|
|
*/
|
|
|
|
public function exists($name)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$this->load($name);
|
2011-02-24 08:46:59 +00:00
|
|
|
} catch (\InvalidArgumentException $e) {
|
2011-01-13 13:58:17 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if this class is able to render the given template.
|
|
|
|
*
|
|
|
|
* @param string $name A template name
|
|
|
|
*
|
2011-01-21 01:39:28 +00:00
|
|
|
* @return Boolean True if this class supports the given resource, false otherwise
|
2011-01-13 13:58:17 +00:00
|
|
|
*/
|
|
|
|
public function supports($name)
|
|
|
|
{
|
2011-02-04 12:04:02 +00:00
|
|
|
if ($name instanceof \Twig_Template) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-01-26 13:53:12 +00:00
|
|
|
$template = $this->parser->parse($name);
|
|
|
|
|
2011-02-10 17:20:44 +00:00
|
|
|
return 'twig' === $template->get('engine');
|
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;
|
|
|
|
}
|
2011-02-04 18:28:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads the given template.
|
|
|
|
*
|
2011-02-10 17:20:44 +00:00
|
|
|
* @param mixed $name A template name or an instance of Twig_Template
|
2011-02-04 18:28:14 +00:00
|
|
|
*
|
|
|
|
* @return \Twig_TemplateInterface A \Twig_TemplateInterface instance
|
|
|
|
*
|
2011-02-10 17:20:44 +00:00
|
|
|
* @throws \InvalidArgumentException if the template does not exist
|
2011-02-04 18:28:14 +00:00
|
|
|
*/
|
|
|
|
protected function load($name)
|
|
|
|
{
|
|
|
|
if ($name instanceof \Twig_Template) {
|
|
|
|
return $name;
|
|
|
|
}
|
|
|
|
|
2011-02-10 17:20:44 +00:00
|
|
|
try {
|
|
|
|
return $this->environment->loadTemplate($name);
|
|
|
|
} catch (\Twig_Error_Loader $e) {
|
|
|
|
throw new \InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
|
|
|
|
}
|
2011-02-04 18:28:14 +00:00
|
|
|
}
|
2011-01-13 13:58:17 +00:00
|
|
|
}
|