8000 [TwigBridge] Added a TwigEngine in the bridge · johannes85/symfony@dbab7e1 · GitHub
[go: up one dir, main page]

Skip to content

Commit dbab7e1

Browse files
committed
[TwigBridge] Added a TwigEngine in the bridge
This TwigEngine implements the interface available in the component. the TwigBridge in TwigBundle now extends this class and provides only the additional methods for the FrameworkBundle interface.
1 parent 275267f commit dbab7e1

File tree

2 files changed

+132
-78
lines changed

2 files changed

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

src/Symfony/Bundle/TwigBundle/TwigEngine.php

Lines changed: 5 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,21 @@
1111

1212
namespace Symfony\Bundle\TwigBundle;
1313

14+
use Symfony\Bridge\Twig\TwigEngine as BaseEngine;
1415
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
1516
use Symfony\Bundle\FrameworkBundle\Templating\GlobalVariables;
1617
use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference;
1718
use Symfony\Component\Templating\TemplateNameParserInterface;
1819
use Symfony\Component\HttpFoundation\Response;
1920
use Symfony\Component\Config\FileLocatorInterface;
20-
use Symfony\Component\Templating\StreamingEngineInterface;
2121

2222
/**
2323
* This engine knows how to render Twig templates.
2424
*
2525
* @author Fabien Potencier <fabien@symfony.com>
2626
*/
27-
class TwigEngine implements EngineInterface, StreamingEngineInterface
27+
class TwigEngine extends BaseEngine implements EngineInterface
2828
{
29-
protected $environment;
30-
protected $parser;
3129
protected $locator;
3230

3331
/**
@@ -40,8 +38,8 @@ class TwigEngine implements EngineInterface, StreamingEngineInterface
4038
*/
4139
public function __construct(\Twig_Environment $environment, TemplateNameParserInterface $parser, FileLocatorInterface $locator, GlobalVariables $globals = null)
4240
{
43-
$this->environment = $environment;
44-
$this->parser = $parser;
41+
parent::__construct($environment, $parser);
42+
4543
$this->locator = $locator;
4644

4745
if (null !== $globals) {
@@ -63,7 +61,7 @@ public function __construct(\Twig_Environment $environment, TemplateNameParserIn
6361
public function render($name, array $parameters = array())
6462
{
6563
try {
66-
return $this->load($name)->render($parameters);
64+
return parent::render($name, $parameters);
6765
} catch (\Twig_Error $e) {
6866
if ($name instanceof TemplateReference) {
6967
try {
@@ -77,55 +75,6 @@ public function render($name, array $parameters = array())
7775
}
7876
}
7977

80-
/**
81-
* Streams a template.
82-
*
83-
* @param mixed $name A template name or a TemplateReferenceInterface instance
84-
* @param array $parameters An array of parameters to pass to the template
85-
*
86-
* @throws \RuntimeException if the template cannot be rendered
87-
*/
88-
public function stream($name, array $parameters = array())
89-
{
90-
$this->load($name)->display($parameters);
91-
}
92-
93-
/**
94-
* Returns true if the template exists.
95-
*
96-
* @param mixed $name A template name
97-
*
98-
* @return Boolean true if the template exists, false otherwise
99-
*/
100-
public function exists($name)
101-
{
102-
try {
103-
$this->load($name);
104-
} catch (\InvalidArgumentException $e) {
105-
return false;
106-
}
107-
108-
return true;
109-
}
110-
111-
/**
112-
* Returns true if this class is able to render the given template.
113-
*
114-
* @param string $name A template name
115-
*
116-
* @return Boolean True if this class supports the given resource, false otherwise
117-
*/
118-
public function supports($name)
119-
{
120-
if ($name instanceof \Twig_Template) {
121-
return true;
122-
}
123-
124-
$template = $this->parser->parse($name);
125-
126-
return 'twig' === $template->get('engine');
127-
}
128-
12978
/**
13079
* Renders a view and returns a Response.
13180
*
@@ -145,26 +94,4 @@ public function renderResponse($view, array $parameters = array(), Response $res
14594

14695
return $response;
14796
}
148-
149-
/**
150-
* Loads the given template.
151-
*
152-
* @param mixed $name A template name or an instance of Twig_Template
153-
*
154-
* @return \Twig_TemplateInterface A \Twig_TemplateInterface instance
155-
*
156-
* @throws \InvalidArgumentException if the template does not exist
157-
*/
158-
protected function load($name)
159-
{
160-
if ($name instanceof \Twig_Template) {
161-
return $name;
162-
}
163-
164-
try {
165-
return $this->environment->loadTemplate($name);
166-
} catch (\Twig_Error_Loader $e) {
167-
throw new \InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
168-
}
169-
}
17097
}

0 commit comments

Comments
 (0)
0