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

Skip to content

[TwigBundle] Moved the setting of the default escaping strategy from the Twig engine to the Twig environment #13141

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 29, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ private function addTwigOptions(ArrayNodeDefinition $rootNode)
$rootNode
->fixXmlConfig('path')
->children()
->scalarNode('autoescape')->end()
->variableNode('autoescape')
->defaultValue(array('Symfony\Bundle\TwigBundle\TwigDefaultEscapingStrategy', 'guess'))
->end()
->scalarNode('autoescape_service')->defaultNull()->end()
->scalarNode('autoescape_service_method')->defaultNull()->end()
->scalarNode('base_template_class')->example('Twig_Template')->end()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,9 @@ public function load(array $configs, ContainerBuilder $container)
}

if (isset($config['autoescape_service']) && isset($config['autoescape_service_method'])) {
$container->findDefinition('templating.engine.twig')->addMethodCall('setDefaultEscapingStrategy', array(array(new Reference($config['autoescape_service']), $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')));
$config['autoescape'] = array(new Reference($config['autoescape_service']), $config['autoescape_service_method']);
}
unset($config['autoescape_service'], $config['autoescape_service_method']);

$container->setParameter('twig.options', $config);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ public function testLoadCustomTemplateEscapingGuesserConfiguration($format)
$this->loadFromFile($container, 'customTemplateEscapingGuesser', $format);
$this->compileContainer($container);

$this->assertTemplateEscapingGuesserDefinition($container, 'my_project.some_bundle.template_escaping_guesser', 'guess');
$options = $container->getParameter('twig.options');
$this->assertEquals(array(new Reference('my_project.some_bundle.template_escaping_guesser'), 'guess'), $options['autoescape']);
}

/**
Expand All @@ -108,7 +109,8 @@ public function testLoadDefaultTemplateEscapingGuesserConfiguration($format)
$this->loadFromFile($container, 'empty', $format);
$this->compileContainer($container);

$this->assertTemplateEscapingGuesserDefinition($container, 'templating.engine.twig', 'guessDefaultEscapingStrategy');
$options = $container->getParameter('twig.options');
$this->assertEquals(array('Symfony\Bundle\TwigBundle\TwigDefaultEscapingStrategy', 'guess'), $options['autoescape']);
}

public function testGlobalsWithDifferentTypesAndValues()
Expand Down Expand Up @@ -219,18 +221,4 @@ private function loadFromFile(ContainerBuilder $container, $file, $format)

$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]);
}
}
}
}
37 changes: 37 additions & 0 deletions src/Symfony/Bundle/TwigBundle/TwigDefaultEscapingStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\TwigBundle;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about putting it in the bridge (or even in Twig itself) as it has nothing specific to the framework ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was indeed thinking about moving it to Twig, but then we should make it flexible as no everyone is ending its template names with .twig. So, different naming strategies should be supported. I will do that in another PR on Twig.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


/**
* @author Fabien Potencier <[email protected]>
*/
class TwigDefaultEscapingStrategy
{
public static function guess($filename)
{
// remove .twig
$filename = substr($filename, 0, -5);

// get the format
$format = substr($filename, strrpos($filename, '.') + 1);

if ('js' === $format) {
return 'js';
}

if ('txt' === $format) {
return false;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about supporting .css.twig too ? Twig supports the css escaping context too.


return 'html';
}
}
23 changes: 8 additions & 15 deletions src/Symfony/Bundle/TwigBundle/TwigEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,21 @@ public function __construct(\Twig_Environment $environment, TemplateNameParserIn
$this->locator = $locator;
}

/**
* @deprecated Deprecated since version 2.3, to be removed in 3.0. Inject the escaping
* strategy on Twig_Environment instead
*/
public function setDefaultEscapingStrategy($strategy)
{
$this->environment->getExtension('escaper')->setDefaultStrategy($strategy);
}

/**
* @deprecated Deprecated since version 2.3, to be removed in 3.0. Use TwigDefaultEscapingStrategy instead.
*/
public function guessDefaultEscapingStrategy($filename)
{
// remove .twig
$filename = substr($filename, 0, -5);

// get the format
$format = substr($filename, strrpos($filename, '.') + 1);

if ('js' === $format) {
return 'js';
}

if ('txt' === $format) {
return false;
}

return 'html';
return TwigDefaultEscapingStrategy::guess($filename);
}

/**
Expand Down