merged branch maxbeutel/pr/custom-template-escaping (PR #7479)
This PR was merged into the master branch.
Discussion
----------
[TwigBundle] Make twig extension handle custom template escaping guesser
I wanted to add a custom template escaping guesser for twig like in http://twig.sensiolabs.org/doc/recipes.html#using-the-template-name-to-set-the-default-escaping-strategy
This pull request allows you register a service id and a method as a custom guesser:
twig:
autoescape: my_service:guess
Documentation is missing for this PR and the unit tests are not that good. I´d work on it if this PR has any chance of getting merged?
| Q | A
| ------------- | ---
| Bug fix? | no
| New feature? | yes
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | none
| License | MIT
| Doc PR | (missing)
Commits
-------
c2c1ed0
make twig extension handle custom template escaping guesser
This commit is contained in:
commit
898a63c8f0
|
@ -1,6 +1,11 @@
|
|||
CHANGELOG
|
||||
=========
|
||||
|
||||
2.3.0
|
||||
-----
|
||||
|
||||
* added option to configure a custom template escaping guesser
|
||||
|
||||
2.2.0
|
||||
-----
|
||||
|
||||
|
|
|
@ -119,6 +119,8 @@ class Configuration implements ConfigurationInterface
|
|||
->fixXmlConfig('path')
|
||||
->children()
|
||||
->scalarNode('autoescape')->end()
|
||||
->scalarNode('autoescape_service')->defaultNull()->end()
|
||||
->scalarNode('autoescape_service_method')->defaultNull()->end()
|
||||
->scalarNode('base_template_class')->example('Twig_Template')->end()
|
||||
->scalarNode('cache')->defaultValue('%kernel.cache_dir%/twig')->end()
|
||||
->scalarNode('charset')->defaultValue('%kernel.charset%')->end()
|
||||
|
|
|
@ -101,8 +101,6 @@ class TwigExtension extends Extension
|
|||
$config['extensions']
|
||||
);
|
||||
|
||||
$container->setParameter('twig.options', $config);
|
||||
|
||||
if ($container->getParameter('kernel.debug')) {
|
||||
$loader->load('debug.xml');
|
||||
|
||||
|
@ -110,10 +108,19 @@ class TwigExtension extends Extension
|
|||
$container->setAlias('debug.templating.engine.twig', 'templating.engine.twig');
|
||||
}
|
||||
|
||||
if (!isset($config['autoescape'])) {
|
||||
if (isset($config['autoescape_service']) && 0 === strpos($config['autoescape_service'], '@') && isset($config['autoescape_service_method'])) {
|
||||
$container->findDefinition('templating.engine.twig')->addMethodCall('setDefaultEscapingStrategy', array(array(new Reference(substr($config['autoescape_service'], 1)), $config['autoescape_service_method'])));
|
||||
|
||||
unset(
|
||||
$config['autoescape_service'],
|
||||
$config['autoescape_service_method']
|
||||
);
|
||||
} elseif (!isset($config['autoescape'])) {
|
||||
$container->findDefinition('templating.engine.twig')->addMethodCall('setDefaultEscapingStrategy', array(array(new Reference('templating.engine.twig'), 'guessDefaultEscapingStrategy')));
|
||||
}
|
||||
|
||||
$container->setParameter('twig.options', $config);
|
||||
|
||||
$this->addClassesToCompile(array(
|
||||
'Twig_Environment',
|
||||
'Twig_Extension',
|
||||
|
|
|
@ -16,6 +16,8 @@
|
|||
|
||||
<xsd:attribute name="auto-reload" type="xsd:string" />
|
||||
<xsd:attribute name="autoescape" type="xsd:string" />
|
||||
<xsd:attribute name="autoescape_service" type="xsd:string" />
|
||||
<xsd:attribute name="autoescape_service_method" type="xsd:string" />
|
||||
<xsd:attribute name="base-template-class" type="xsd:string" />
|
||||
<xsd:attribute name="cache" type="xsd:string" />
|
||||
<xsd:attribute name="charset" type="xsd:string" />
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
<?php
|
||||
|
||||
$container->loadFromExtension('twig', array(
|
||||
'autoescape_service' => '@my_project.some_bundle.template_escaping_guesser',
|
||||
'autoescape_service_method' => 'guess',
|
||||
));
|
|
@ -0,0 +1,3 @@
|
|||
<?php
|
||||
|
||||
$container->loadFromExtension('twig', array());
|
|
@ -0,0 +1,10 @@
|
|||
<?xml version="1.0" ?>
|
||||
|
||||
<container xmlns="http://symfony.com/schema/dic/services"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:twig="http://symfony.com/schema/dic/twig"
|
||||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
|
||||
http://symfony.com/schema/dic/twig http://symfony.com/schema/dic/twig/twig-1.0.xsd">
|
||||
|
||||
<twig:config autoescape_service="@my_project.some_bundle.template_escaping_guesser" autoescape_service_method="guess" />
|
||||
</container>
|
|
@ -0,0 +1,10 @@
|
|||
<?xml version="1.0" ?>
|
||||
|
||||
<container xmlns="http://symfony.com/schema/dic/services"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:twig="http://symfony.com/schema/dic/twig"
|
||||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
|
||||
http://symfony.com/schema/dic/twig http://symfony.com/schema/dic/twig/twig-1.0.xsd">
|
||||
|
||||
<twig:config />
|
||||
</container>
|
|
@ -0,0 +1,3 @@
|
|||
twig:
|
||||
autoescape_service: @my_project.some_bundle.template_escaping_guesser
|
||||
autoescape_service_method: guess
|
|
@ -0,0 +1 @@
|
|||
twig:
|
|
@ -83,6 +83,32 @@ class TwigExtensionTest extends TestCase
|
|||
$this->assertTrue($options['strict_variables'], '->load() sets the strict_variables option');
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getFormats
|
||||
*/
|
||||
public function testLoadCustomTemplateEscapingGuesserConfiguration($format)
|
||||
{
|
||||
$container = $this->createContainer();
|
||||
$container->registerExtension(new TwigExtension());
|
||||
$this->loadFromFile($container, 'customTemplateEscapingGuesser', $format);
|
||||
$this->compileContainer($container);
|
||||
|
||||
$this->assertTemplateEscapingGuesserDefinition($container, 'my_project.some_bundle.template_escaping_guesser', 'guess');
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getFormats
|
||||
*/
|
||||
public function testLoadDefaultTemplateEscapingGuesserConfiguration($format)
|
||||
{
|
||||
$container = $this->createContainer();
|
||||
$container->registerExtension(new TwigExtension());
|
||||
$this->loadFromFile($container, 'empty', $format);
|
||||
$this->compileContainer($container);
|
||||
|
||||
$this->assertTemplateEscapingGuesserDefinition($container, 'templating.engine.twig', 'guessDefaultEscapingStrategy');
|
||||
}
|
||||
|
||||
public function testGlobalsWithDifferentTypesAndValues()
|
||||
{
|
||||
$globals = array(
|
||||
|
@ -189,4 +215,18 @@ class TwigExtensionTest extends TestCase
|
|||
|
||||
$loader->load($file.'.'.$format);
|
||||
}
|
||||
|
||||
private function assertTemplateEscapingGuesserDefinition(ContainerBuilder $container, $serviceId, $serviceMethod)
|
||||
{
|
||||
$def = $container->getDefinition('templating.engine.twig');
|
||||
|
||||
$this->assertCount(1, $def->getMethodCalls());
|
||||
|
||||
foreach ($def->getMethodCalls() as $call) {
|
||||
if ('setDefaultEscapingStrategy' === $call[0]) {
|
||||
$this->assertSame($serviceId, (string) $call[1][0][0]);
|
||||
$this->assertSame($serviceMethod, $call[1][0][1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Reference in New Issue