vendor/symfony/src/Symfony/Bundle/TwigBundle/TwigEngine.php
changeset 0 7f95f8617b0b
equal deleted inserted replaced
-1:000000000000 0:7f95f8617b0b
       
     1 <?php
       
     2 
       
     3 /*
       
     4  * This file is part of the Symfony package.
       
     5  *
       
     6  * (c) Fabien Potencier <fabien@symfony.com>
       
     7  *
       
     8  * For the full copyright and license information, please view the LICENSE
       
     9  * file that was distributed with this source code.
       
    10  */
       
    11 
       
    12 namespace Symfony\Bundle\TwigBundle;
       
    13 
       
    14 use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
       
    15 use Symfony\Bundle\FrameworkBundle\Templating\GlobalVariables;
       
    16 use Symfony\Component\Templating\TemplateNameParserInterface;
       
    17 use Symfony\Component\HttpFoundation\Response;
       
    18 use Symfony\Component\DependencyInjection\ContainerInterface;
       
    19 
       
    20 /**
       
    21  * This engine knows how to render Twig templates.
       
    22  *
       
    23  * @author Fabien Potencier <fabien@symfony.com>
       
    24  */
       
    25 class TwigEngine implements EngineInterface
       
    26 {
       
    27     protected $environment;
       
    28     protected $parser;
       
    29 
       
    30     /**
       
    31      * Constructor.
       
    32      *
       
    33      * @param \Twig_Environment           $environment A \Twig_Environment instance
       
    34      * @param TemplateNameParserInterface $parser      A TemplateNameParserInterface instance
       
    35      * @param GlobalVariables|null        $globals     A GlobalVariables instance or null
       
    36      */
       
    37     public function __construct(\Twig_Environment $environment, TemplateNameParserInterface $parser, GlobalVariables $globals = null)
       
    38     {
       
    39         $this->environment = $environment;
       
    40         $this->parser = $parser;
       
    41 
       
    42         if (null !== $globals) {
       
    43             $environment->addGlobal('app', $globals);
       
    44         }
       
    45     }
       
    46 
       
    47     /**
       
    48      * Renders a template.
       
    49      *
       
    50      * @param mixed $name       A template name
       
    51      * @param array $parameters An array of parameters to pass to the template
       
    52      *
       
    53      * @return string The evaluated template as a string
       
    54      *
       
    55      * @throws \InvalidArgumentException if the template does not exist
       
    56      * @throws \RuntimeException         if the template cannot be rendered
       
    57      */
       
    58     public function render($name, array $parameters = array())
       
    59     {
       
    60         return $this->load($name)->render($parameters);
       
    61     }
       
    62 
       
    63     /**
       
    64      * Returns true if the template exists.
       
    65      *
       
    66      * @param mixed $name A template name
       
    67      *
       
    68      * @return Boolean true if the template exists, false otherwise
       
    69      */
       
    70     public function exists($name)
       
    71     {
       
    72         try {
       
    73             $this->load($name);
       
    74         } catch (\InvalidArgumentException $e) {
       
    75             return false;
       
    76         }
       
    77 
       
    78         return true;
       
    79     }
       
    80 
       
    81     /**
       
    82      * Returns true if this class is able to render the given template.
       
    83      *
       
    84      * @param string $name A template name
       
    85      *
       
    86      * @return Boolean True if this class supports the given resource, false otherwise
       
    87      */
       
    88     public function supports($name)
       
    89     {
       
    90         if ($name instanceof \Twig_Template) {
       
    91             return true;
       
    92         }
       
    93 
       
    94         $template = $this->parser->parse($name);
       
    95 
       
    96         return 'twig' === $template->get('engine');
       
    97     }
       
    98 
       
    99     /**
       
   100      * Renders a view and returns a Response.
       
   101      *
       
   102      * @param string   $view       The view name
       
   103      * @param array    $parameters An array of parameters to pass to the view
       
   104      * @param Response $response   A Response instance
       
   105      *
       
   106      * @return Response A Response instance
       
   107      */
       
   108     public function renderResponse($view, array $parameters = array(), Response $response = null)
       
   109     {
       
   110         if (null === $response) {
       
   111             $response = new Response();
       
   112         }
       
   113 
       
   114         $response->setContent($this->render($view, $parameters));
       
   115 
       
   116         return $response;
       
   117     }
       
   118 
       
   119     /**
       
   120      * Loads the given template.
       
   121      *
       
   122      * @param mixed $name A template name or an instance of Twig_Template
       
   123      *
       
   124      * @return \Twig_TemplateInterface A \Twig_TemplateInterface instance
       
   125      *
       
   126      * @throws \InvalidArgumentException if the template does not exist
       
   127      */
       
   128     protected function load($name)
       
   129     {
       
   130         if ($name instanceof \Twig_Template) {
       
   131             return $name;
       
   132         }
       
   133 
       
   134         try {
       
   135             return $this->environment->loadTemplate($name);
       
   136         } catch (\Twig_Error_Loader $e) {
       
   137             throw new \InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
       
   138         }
       
   139     }
       
   140 }