]> git.immae.eu Git - github/wallabag/wallabag.git/blob - vendor/symfony/twig-bridge/Symfony/Bridge/Twig/TwigEngine.php
twig implementation
[github/wallabag/wallabag.git] / vendor / symfony / twig-bridge / Symfony / Bridge / Twig / TwigEngine.php
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\Bridge\Twig;
13
14 use Symfony\Component\Templating\EngineInterface;
15 use Symfony\Component\Templating\StreamingEngineInterface;
16 use Symfony\Component\Templating\TemplateNameParserInterface;
17
18 /**
19 * This engine knows how to render Twig templates.
20 *
21 * @author Fabien Potencier <fabien@symfony.com>
22 */
23 class TwigEngine implements EngineInterface, StreamingEngineInterface
24 {
25 protected $environment;
26 protected $parser;
27
28 /**
29 * Constructor.
30 *
31 * @param \Twig_Environment $environment A \Twig_Environment instance
32 * @param TemplateNameParserInterface $parser A TemplateNameParserInterface instance
33 */
34 public function __construct(\Twig_Environment $environment, TemplateNameParserInterface $parser)
35 {
36 $this->environment = $environment;
37 $this->parser = $parser;
38 }
39
40 /**
41 * Renders a template.
42 *
43 * @param mixed $name A template name
44 * @param array $parameters An array of parameters to pass to the template
45 *
46 * @return string The evaluated template as a string
47 *
48 * @throws \InvalidArgumentException if the template does not exist
49 * @throws \RuntimeException if the template cannot be rendered
50 */
51 public function render($name, array $parameters = array())
52 {
53 return $this->load($name)->render($parameters);
54 }
55
56 /**
57 * Streams a template.
58 *
59 * @param mixed $name A template name or a TemplateReferenceInterface instance
60 * @param array $parameters An array of parameters to pass to the template
61 *
62 * @throws \RuntimeException if the template cannot be rendered
63 */
64 public function stream($name, array $parameters = array())
65 {
66 $this->load($name)->display($parameters);
67 }
68
69 /**
70 * Returns true if the template exists.
71 *
72 * @param mixed $name A template name
73 *
74 * @return Boolean true if the template exists, false otherwise
75 */
76 public function exists($name)
77 {
78 try {
79 $this->load($name);
80 } catch (\InvalidArgumentException $e) {
81 return false;
82 }
83
84 return true;
85 }
86
87 /**
88 * Returns true if this class is able to render the given template.
89 *
90 * @param string $name A template name
91 *
92 * @return Boolean True if this class supports the given resource, false otherwise
93 */
94 public function supports($name)
95 {
96 if ($name instanceof \Twig_Template) {
97 return true;
98 }
99
100 $template = $this->parser->parse($name);
101
102 return 'twig' === $template->get('engine');
103 }
104
105 /**
106 * Loads the given template.
107 *
108 * @param mixed $name A template name or an instance of Twig_Template
109 *
110 * @return \Twig_TemplateInterface A \Twig_TemplateInterface instance
111 *
112 * @throws \InvalidArgumentException if the template does not exist
113 */
114 protected function load($name)
115 {
116 if ($name instanceof \Twig_Template) {
117 return $name;
118 }
119
120 try {
121 return $this->environment->loadTemplate($name);
122 } catch (\Twig_Error_Loader $e) {
123 throw new \InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
124 }
125 }
126 }