/
TemplatingExtension.php
191 lines (158 loc) · 5.86 KB
/
TemplatingExtension.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
namespace Symfony\Bundle\TwigBundle\Extension;
use Symfony\Bundle\TwigBundle\TokenParser\HelperTokenParser;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Bundle\TwigBundle\TokenParser\IncludeTokenParser;
use Symfony\Bundle\TwigBundle\TokenParser\UrlTokenParser;
use Symfony\Bundle\TwigBundle\TokenParser\PathTokenParser;
use Symfony\Bundle\TwigBundle\TokenParser\RenderTokenParser;
use Symfony\Component\Yaml\Dumper as YamlDumper;
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
*
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
class TemplatingExtension extends \Twig_Extension
{
protected $container;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
public function getContainer()
{
return $this->container;
}
public function getTemplating()
{
return $this->container->get('templating.engine');
}
/**
* {@inheritdoc}
*/
public function getFilters()
{
return array(
'yaml_encode' => new \Twig_Filter_Method($this, 'yamlEncode'),
'dump' => new \Twig_Filter_Method($this, 'dump'),
'abbr_class' => new \Twig_Filter_Method($this, 'abbrClass', array('is_safe' => array('html'))),
'abbr_method' => new \Twig_Filter_Method($this, 'abbrMethod', array('is_safe' => array('html'))),
'format_args' => new \Twig_Filter_Method($this, 'formatArgs', array('is_safe' => array('html'))),
'format_args_as_text' => new \Twig_Filter_Method($this, 'formatArgsAsText', array('is_safe' => array('html'))),
'file_excerpt' => new \Twig_Filter_Method($this, 'fileExcerpt', array('is_safe' => array('html'))),
'format_file' => new \Twig_Filter_Method($this, 'formatFile', array('is_safe' => array('html'))),
'format_file_from_text' => new \Twig_Filter_Method($this, 'formatFileFromText', array('is_safe' => array('html'))),
);
}
/**
* Returns a list of functions to add to the existing list.
*
* @return array An array of functions
*/
public function getFunctions()
{
return array(
'url' => new \Twig_Function_Method($this, 'getUrl'),
'path' => new \Twig_Function_Method($this, 'getPath'),
'asset' => new \Twig_Function_Method($this, 'getAssetUrl'),
);
}
public function getPath($name, array $parameters = array())
{
return $this->container->get('router')->generate($name, $parameters, false);
}
public function getUrl($name, array $parameters = array())
{
return $this->container->get('router')->generate($name, $parameters, true);
}
public function getAssetUrl($location)
{
return $this->container->get('templating.helper.assets')->getUrl($location);
}
/**
* Returns the token parser instance to add to the existing list.
*
* @return array An array of Twig_TokenParser instances
*/
public function getTokenParsers()
{
return array(
// {% javascript 'bundles/blog/js/blog.js' %}
new HelperTokenParser('javascript', '<js> [with <arguments:hash>]', 'templating.helper.javascripts', 'add'),
// {% javascripts %}
new HelperTokenParser('javascripts', '', 'templating.helper.javascripts', 'render'),
// {% stylesheet 'bundles/blog/css/blog.css' with { 'media': 'screen' } %}
new HelperTokenParser('stylesheet', '<css> [with <arguments:hash>]', 'templating.helper.stylesheets', 'add'),
// {% stylesheets %}
new HelperTokenParser('stylesheets', '', 'templating.helper.stylesheets', 'render'),
// {% render 'BlogBundle:Post:list' with { 'limit': 2 }, { 'alt': 'BlogBundle:Post:error' } %}
new RenderTokenParser(),
// {% flash 'notice' %}
new HelperTokenParser('flash', '<name>', 'templating.helper.session', 'getFlash'),
// {% include 'sometemplate.php' with { 'something' : 'something2' } %}
new IncludeTokenParser(),
);
}
public function yamlEncode($input, $inline = 0)
{
static $dumper;
if (null === $dumper) {
$dumper = new YamlDumper();
}
return $dumper->dump($input, $inline);
}
public function abbrClass($class)
{
return $this->getTemplating()->get('code')->abbrClass($class);
}
public function abbrMethod($method)
{
return $this->getTemplating()->get('code')->abbrMethod($method);
}
public function formatArgs($args)
{
return $this->getTemplating()->get('code')->formatArgs($args);
}
public function formatArgsAsText($args)
{
return $this->getTemplating()->get('code')->formatArgsAsText($args);
}
public function fileExcerpt($file, $line)
{
return $this->getTemplating()->get('code')->fileExcerpt($file, $line);
}
public function formatFile($file, $line)
{
return $this->getTemplating()->get('code')->formatFile($file, $line);
}
public function formatFileFromText($text)
{
return $this->getTemplating()->get('code')->formatFileFromText($text);
}
public function dump($value)
{
if (is_resource($value)) {
return '%Resource%';
}
if (is_array($value) || is_object($value)) {
return '%'.gettype($value).'% '.$this->yamlEncode($value);
}
return $value;
}
/**
* Returns the name of the extension.
*
* @return string The extension name
*/
public function getName()
{
return 'templating';
}
}