Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[TwigBundle] Add "use_yield" option to allow opting in for this new rendering mode and skip a deprecation #53805

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

Closed
wants to merge 1 commit into from
Closed
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
[TwigBundle] Add "use_yield" option to allow opting in for this new r…
…endering mode and skip a deprecation
  • Loading branch information
nicolas-grekas committed Feb 6, 2024
commit 439e0d665284ea0acd453263347f0841f2b988d7
9 changes: 7 additions & 2 deletions 9 src/Symfony/Bridge/Twig/Form/TwigRendererEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Form\FormView;
use Twig\Environment;
use Twig\Template;
use Twig\YieldingTemplate;

/**
* @author Bernhard Schussek <bschussek@gmail.com>
Expand Down Expand Up @@ -46,13 +47,17 @@ public function renderBlock(FormView $view, $resource, string $blockName, array

$context = $this->environment->mergeGlobals($variables);

ob_start();

// By contract,This method can only be called after getting the resource
// (which is passed to the method). Getting a resource for the first time
// (with an empty cache) is guaranteed to invoke loadResourcesFromTheme(),
// where the property $template is initialized.

if ($this->template instanceof YieldingTemplate) {
Copy link
Member

Choose a reason for hiding this comment

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

Do we actually still need to use the internal class Template in $this->template, forcing us to use more internal classes there ?
Couldn't we instead rely on the public API of TemplateWrapper, which has a renderBlock method (and btw, meging globals would also be useless as Twig would be doing it in its public entry points)

return $this->template->renderBlock($blockName, $context, $this->resources[$cacheKey]);
}

ob_start();

// We do not call renderBlock here to avoid too many nested level calls
// (XDebug limits the level to 100 by default)
$this->template->displayBlock($blockName, $context, $this->resources[$cacheKey]);
Expand Down
6 changes: 3 additions & 3 deletions 6 src/Symfony/Bridge/Twig/Tests/Command/DebugCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public static function getDebugTemplateNameTestData()
(None) templates%e%A
%A
@Twig templates/bundles/TwigBundle%e%A
vendors/twig-bundle/Resources/views%e%A
vendors/twig-bundle/Resources/views%e%A
----------- -------------------------------------%A


Expand Down Expand Up @@ -305,7 +305,7 @@ public function testComplete(array $input, array $expectedSuggestions)

$projectDir = \dirname(__DIR__).\DIRECTORY_SEPARATOR.'Fixtures';
$loader = new FilesystemLoader([], $projectDir);
$environment = new Environment($loader);
$environment = new Environment($loader, ['use_yield' => true]);

$application = new Application();
$application->add(new DebugCommand($environment, $projectDir, [], null, null));
Expand Down Expand Up @@ -337,7 +337,7 @@ private function createCommandTester(array $paths = [], array $bundleMetadata =
$loader = new ChainLoader([$loader]);
}

$environment = new Environment($loader);
$environment = new Environment($loader, ['use_yield' => true]);
foreach ($globals as $name => $value) {
$environment->addGlobal($name, $value);
}
Expand Down
2 changes: 1 addition & 1 deletion 2 src/Symfony/Bridge/Twig/Tests/Command/LintCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ private function createCommandTester(): CommandTester

private function createCommand(): Command
{
$environment = new Environment(new FilesystemLoader(\dirname(__DIR__).'/Fixtures/templates/'));
$environment = new Environment(new FilesystemLoader(\dirname(__DIR__).'/Fixtures/templates/'), ['use_yield' => true]);
$environment->addFilter(new TwigFilter('deprecated_filter', function ($v) {
return $v;
}, ['deprecated' => true]));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function testFallbackToNativeRendererIfCustomTemplateNotFound()
{
$exception = new NotFoundHttpException();

$twig = new Environment(new ArrayLoader([]));
$twig = new Environment(new ArrayLoader([]), ['use_yield' => true]);

$nativeRenderer = $this->createMock(HtmlErrorRenderer::class);
$nativeRenderer
Expand All @@ -57,7 +57,7 @@ public function testRenderCustomErrorTemplate()
{
$twig = new Environment(new ArrayLoader([
'@Twig/Exception/error404.html.twig' => '<h1>Page Not Found</h1>',
]));
]), ['use_yield' => true]);
$exception = (new TwigErrorRenderer($twig))->render(new NotFoundHttpException());

$this->assertSame('<h1>Page Not Found</h1>', $exception->getAsString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ public function testFormatArgsIntegration()
$this->assertEquals($expected, $this->render($template, $data));
}


public function testFormatFileIntegration()
{
$template = <<<'TWIG'
Expand Down Expand Up @@ -156,7 +155,7 @@ private function render(string $template, array $context = [])
{
$twig = new Environment(
new ArrayLoader(['index' => $template]),
['debug' => true]
['debug' => true, 'use_yield' => true]
);
$twig->addExtension($this->getExtension());

Expand Down
3 changes: 3 additions & 0 deletions 3 src/Symfony/Bridge/Twig/Tests/Extension/DumpExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function testDumpTag($template, $debug, $expectedOutput, $expectedDumped)
'debug' => $debug,
'cache' => false,
'optimizations' => 0,
'use_yield' => true,
]);
$twig->addExtension($extension);

Expand Down Expand Up @@ -72,6 +73,7 @@ public function testDump($context, $args, $expectedOutput, $debug = true)
'debug' => $debug,
'cache' => false,
'optimizations' => 0,
'use_yield' => true,
]);

array_unshift($args, $context);
Expand Down Expand Up @@ -129,6 +131,7 @@ public function testCustomDumper()
'debug' => true,
'cache' => false,
'optimizations' => 0,
'use_yield' => true,
]);

$dump = $extension->dump($twig, [], 'foo');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class ExpressionExtensionTest extends TestCase
public function testExpressionCreation()
{
$template = "{{ expression('1 == 1') }}";
$twig = new Environment(new ArrayLoader(['template' => $template]), ['debug' => true, 'cache' => false, 'autoescape' => 'html', 'optimizations' => 0]);
$twig = new Environment(new ArrayLoader(['template' => $template]), ['debug' => true, 'cache' => false, 'autoescape' => 'html', 'optimizations' => 0, 'use_yield' => true]);
$twig->addExtension(new ExpressionExtension());

$output = $twig->render('template');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected function setUp(): void
__DIR__.'/Fixtures/templates/form',
]);

$environment = new Environment($loader, ['strict_variables' => true]);
$environment = new Environment($loader, ['strict_variables' => true, 'use_yield' => true]);
$environment->addExtension(new TranslationExtension(new StubTranslator()));
$environment->addExtension(new FormExtension());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ protected function setUp(): void
__DIR__.'/Fixtures/templates/form',
]);

$environment = new Environment($loader, ['strict_variables' => true]);
$environment = new Environment($loader, ['strict_variables' => true, 'use_yield' => true]);
$environment->addExtension(new TranslationExtension(new StubTranslator()));
$environment->addExtension(new FormExtension());

Expand Down Expand Up @@ -80,7 +80,7 @@ public function testMoneyWidgetInIso()
$environment = new Environment(new FilesystemLoader([
__DIR__.'/../../Resources/views/Form',
__DIR__.'/Fixtures/templates/form',
]), ['strict_variables' => true]);
]), ['strict_variables' => true, 'use_yield' => true]);
$environment->addExtension(new TranslationExtension(new StubTranslator()));
$environment->addExtension(new FormExtension());
$environment->setCharset('ISO-8859-1');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected function setUp(): void
__DIR__.'/Fixtures/templates/form',
]);

$environment = new Environment($loader, ['strict_variables' => true]);
$environment = new Environment($loader, ['strict_variables' => true, 'use_yield' => true]);
$environment->addExtension(new TranslationExtension(new StubTranslator()));
$environment->addExtension(new FormExtension());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected function setUp(): void
__DIR__.'/Fixtures/templates/form',
]);

$environment = new Environment($loader, ['strict_variables' => true]);
$environment = new Environment($loader, ['strict_variables' => true, 'use_yield' => true]);
$environment->addExtension(new TranslationExtension(new StubTranslator()));
$environment->addExtension(new FormExtension());

Expand Down Expand Up @@ -84,7 +84,7 @@ public function testMoneyWidgetInIso()
$environment = new Environment(new FilesystemLoader([
__DIR__.'/../../Resources/views/Form',
__DIR__.'/Fixtures/templates/form',
]), ['strict_variables' => true]);
]), ['strict_variables' => true, 'use_yield' => true]);
$environment->addExtension(new TranslationExtension(new StubTranslator()));
$environment->addExtension(new FormExtension());
$environment->setCharset('ISO-8859-1');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected function setUp(): void
__DIR__.'/Fixtures/templates/form',
]);

$environment = new Environment($loader, ['strict_variables' => true]);
$environment = new Environment($loader, ['strict_variables' => true, 'use_yield' => true]);
$environment->addExtension(new TranslationExtension(new StubTranslator()));
$environment->addExtension(new FormExtension());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ protected function setUp(): void
__DIR__.'/Fixtures/templates/form',
]);

$environment = new Environment($loader, ['strict_variables' => true]);
$environment = new Environment($loader, ['strict_variables' => true, 'use_yield' => true]);
$environment->addExtension(new TranslationExtension(new StubTranslator()));
$environment->addExtension(new FormExtension());

Expand Down Expand Up @@ -87,7 +87,7 @@ public function testMoneyWidgetInIso()
$environment = new Environment(new FilesystemLoader([
__DIR__.'/../../Resources/views/Form',
__DIR__.'/Fixtures/templates/form',
]), ['strict_variables' => true]);
]), ['strict_variables' => true, 'use_yield' => true]);
$environment->addExtension(new TranslationExtension(new StubTranslator()));
$environment->addExtension(new FormExtension());
$environment->setCharset('ISO-8859-1');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ protected function setUp(): void
__DIR__.'/Fixtures/templates/form',
]);

$environment = new Environment($loader, ['strict_variables' => true]);
$environment = new Environment($loader, ['strict_variables' => true, 'use_yield' => true]);
$environment->addExtension(new TranslationExtension(new StubTranslator()));
$environment->addGlobal('global', '');
// the value can be any template that exists
Expand Down Expand Up @@ -171,7 +171,7 @@ public function testMoneyWidgetInIso()
$environment = new Environment(new FilesystemLoader([
__DIR__.'/../../Resources/views/Form',
__DIR__.'/Fixtures/templates/form',
]), ['strict_variables' => true]);
]), ['strict_variables' => true, 'use_yield' => true]);
$environment->addExtension(new TranslationExtension(new StubTranslator()));
$environment->addExtension(new FormExtension());
$environment->setCharset('ISO-8859-1');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected function setUp(): void
__DIR__.'/Fixtures/templates/form',
]);

$environment = new Environment($loader, ['strict_variables' => true]);
$environment = new Environment($loader, ['strict_variables' => true, 'use_yield' => true]);
$environment->addExtension(new TranslationExtension(new StubTranslator()));
$environment->addGlobal('global', '');
$environment->addExtension(new FormExtension());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function testGenerateFragmentUri()
{{ fragment_uri(controller("%s::templateAction", {template: "foo.html.twig"})) }}
TWIG
, TemplateController::class), ]);
$twig = new Environment($loader, ['debug' => true, 'cache' => false]);
$twig = new Environment($loader, ['debug' => true, 'cache' => false, 'use_yield' => true]);
$twig->addExtension(new HttpKernelExtension());

$loader = $this->createMock(RuntimeLoaderInterface::class);
Expand Down Expand Up @@ -103,7 +103,7 @@ protected function getFragmentHandler($return)
protected function renderTemplate(FragmentHandler $renderer, $template = '{{ render("foo") }}')
{
$loader = new ArrayLoader(['index' => $template]);
$twig = new Environment($loader, ['debug' => true, 'cache' => false]);
$twig = new Environment($loader, ['debug' => true, 'cache' => false, 'use_yield' => true]);
$twig->addExtension(new HttpKernelExtension());

$loader = $this->createMock(RuntimeLoaderInterface::class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class RoutingExtensionTest extends TestCase
*/
public function testEscaping($template, $mustBeEscaped)
{
$twig = new Environment($this->createMock(LoaderInterface::class), ['debug' => true, 'cache' => false, 'autoescape' => 'html', 'optimizations' => 0]);
$twig = new Environment($this->createMock(LoaderInterface::class), ['debug' => true, 'cache' => false, 'autoescape' => 'html', 'optimizations' => 0, 'use_yield' => true]);
$twig->addExtension(new RoutingExtension($this->createMock(UrlGeneratorInterface::class)));

$nodes = $twig->parse($twig->tokenize(new Source($template, '')));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private function getTwig(string $template): Environment
])
;

$twig = new Environment(new ArrayLoader(['template' => $template]));
$twig = new Environment(new ArrayLoader(['template' => $template, 'use_yield' => true]));
$twig->addExtension(new SerializerExtension());
$twig->addRuntimeLoader($mockRuntimeLoader);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function testFailIfStoppingWrongEvent()
*/
public function testTiming($template, $events)
{
$twig = new Environment(new ArrayLoader(['template' => $template]), ['debug' => true, 'cache' => false, 'autoescape' => 'html', 'optimizations' => 0]);
$twig = new Environment(new ArrayLoader(['template' => $template]), ['debug' => true, 'cache' => false, 'autoescape' => 'html', 'optimizations' => 0, 'use_yield' => true]);
$twig->addExtension(new StopwatchExtension($this->getStopwatch($events)));

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function testTrans($template, $expected, array $variables = [])
if ($expected != $this->getTemplate($template)->render($variables)) {
echo $template."\n";
$loader = new TwigArrayLoader(['index' => $template]);
$twig = new Environment($loader, ['debug' => true, 'cache' => false]);
$twig = new Environment($loader, ['debug' => true, 'cache' => false, 'use_yield' => true]);
$twig->addExtension(new TranslationExtension(new Translator('en')));

echo $twig->compile($twig->parse($twig->tokenize($twig->getLoader()->getSourceContext('index'))))."\n\n";
Expand Down Expand Up @@ -217,7 +217,7 @@ private function getTemplate($template, ?TranslatorInterface $translator = null)
} else {
$loader = new TwigArrayLoader(['index' => $template]);
}
$twig = new Environment($loader, ['debug' => true, 'cache' => false]);
$twig = new Environment($loader, ['debug' => true, 'cache' => false, 'use_yield' => true]);
$twig->addExtension(new TranslationExtension($translator));

return $twig->load('index');
Expand Down
6 changes: 3 additions & 3 deletions 6 src/Symfony/Bridge/Twig/Tests/Mime/BodyRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function testRenderedOnce()
{
$twig = new Environment(new ArrayLoader([
'text' => 'Text',
]));
]), ['use_yield' => true]);
$renderer = new BodyRenderer($twig);
$email = (new TemplatedEmail())
->to('fabien@symfony.com')
Expand All @@ -104,7 +104,7 @@ public function testRenderedOnceUnserializableContext()
{
$twig = new Environment(new ArrayLoader([
'text' => 'Text',
]));
]), ['use_yield' => true]);
$renderer = new BodyRenderer($twig);
$email = (new TemplatedEmail())
->to('fabien@symfony.com')
Expand All @@ -128,7 +128,7 @@ private function prepareEmail(?string $text, ?string $html, array $context = [])
'html' => $html,
'document.txt' => 'Some text document...',
'image.jpg' => 'Some image data',
]));
]), ['use_yield' => true]);
$renderer = new BodyRenderer($twig);
$email = (new TemplatedEmail())
->to('fabien@symfony.com')
Expand Down
8 changes: 4 additions & 4 deletions 8 src/Symfony/Bridge/Twig/Tests/Node/DumpNodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function testNoVar()
{
$node = new DumpNode('bar', null, 7);

$env = new Environment($this->createMock(LoaderInterface::class));
$env = new Environment($this->createMock(LoaderInterface::class), ['use_yield' => true]);
$compiler = new Compiler($env);

$expected = <<<'EOTXT'
Expand All @@ -49,7 +49,7 @@ public function testIndented()
{
$node = new DumpNode('bar', null, 7);

$env = new Environment($this->createMock(LoaderInterface::class));
$env = new Environment($this->createMock(LoaderInterface::class), ['use_yield' => true]);
$compiler = new Compiler($env);

$expected = <<<'EOTXT'
Expand All @@ -76,7 +76,7 @@ public function testOneVar()
]);
$node = new DumpNode('bar', $vars, 7);

$env = new Environment($this->createMock(LoaderInterface::class));
$env = new Environment($this->createMock(LoaderInterface::class), ['use_yield' => true]);
$compiler = new Compiler($env);

$expected = <<<'EOTXT'
Expand All @@ -100,7 +100,7 @@ public function testMultiVars()
]);
$node = new DumpNode('bar', $vars, 7);

$env = new Environment($this->createMock(LoaderInterface::class));
$env = new Environment($this->createMock(LoaderInterface::class), ['use_yield' => true]);
$compiler = new Compiler($env);

$expected = <<<'EOTXT'
Expand Down
2 changes: 1 addition & 1 deletion 2 src/Symfony/Bridge/Twig/Tests/Node/FormThemeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function testCompile()

$node = new FormThemeNode($form, $resources, 0);

$environment = new Environment($this->createMock(LoaderInterface::class));
$environment = new Environment($this->createMock(LoaderInterface::class), ['use_yield' => true]);
$formRenderer = new FormRenderer($this->createMock(FormRendererEngineInterface::class));
$this->registerTwigRuntimeLoader($environment, $formRenderer);
$compiler = new Compiler($environment);
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.