Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e369d14

Browse files
committed
added a Twig extension to create Expression instances
1 parent 38b7fde commit e369d14

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\Extension;
13+
14+
use Symfony\Component\ExpressionLanguage\Expression;
15+
16+
/**
17+
* ExpressionExtension gives a way to create Expressions from a template.
18+
*
19+
* @author Fabien Potencier <[email protected]>
20+
*/
21+
class ExpressionExtension extends \Twig_Extension
22+
{
23+
/**
24+
* {@inheritdoc}
25+
*/
26+
public function getFunctions()
27+
{
28+
return array(
29+
new \Twig_SimpleFunction('expression', array($this, 'createExpression')),
30+
);
31+
}
32+
33+
private function createExpression($expression)
34+
{
35+
return new Expression($expression);
36+
}
37+
38+
/**
39+
* Returns the name of the extension.
40+
*
41+
* @return string The extension name
42+
*/
43+
public function getName()
44+
{
45+
return 'expression';
46+
}
47+
}

src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
<parameter key="twig.extension.form.class">Symfony\Bridge\Twig\Extension\FormExtension</parameter>
2020
<parameter key="twig.extension.httpkernel.class">Symfony\Bridge\Twig\Extension\HttpKernelExtension</parameter>
2121
<parameter key="twig.extension.debug.stopwatch.class">Symfony\Bridge\Twig\Extension\StopwatchExtension</parameter>
22+
<parameter key="twig.extension.expression.class">Symfony\Bridge\Twig\Extension\ExpressionExtension</parameter>
2223
<parameter key="twig.form.engine.class">Symfony\Bridge\Twig\Form\TwigRendererEngine</parameter>
2324
<parameter key="twig.form.renderer.class">Symfony\Bridge\Twig\Form\TwigRenderer</parameter>
2425
<parameter key="twig.translation.extractor.class">Symfony\Bridge\Twig\Translation\TwigExtractor</parameter>
@@ -92,6 +93,10 @@
9293
<argument type="service" id="debug.stopwatch" on-invalid="ignore" />
9394
</service>
9495

96+
<service id="twig.extension.expression" class="%twig.extension.expression.class%" public="false">
97+
<tag name="twig.extension" />
98+
</service>
99+
95100
<service id="twig.extension.httpkernel" class="%twig.extension.httpkernel.class%" public="false">
96101
<argument type="service" id="fragment.handler" />
97102
</service>

0 commit comments

Comments
 (0)