Skip to content

[TwigBridge] Render a block via the #[Template] attribute #58028

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
Aug 19, 2024
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
2 changes: 2 additions & 0 deletions src/Symfony/Bridge/Twig/Attribute/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ class Template
* @param string $template The name of the template to render
* @param string[]|null $vars The controller method arguments to pass to the template
* @param bool $stream Enables streaming the template
* @param string|null $block The name of the block to use in the template
*/
public function __construct(
public string $template,
public ?array $vars = null,
public bool $stream = false,
public ?string $block = null,
) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,16 @@ public function onKernelView(ViewEvent $event): void
}

$event->setResponse($attribute->stream
? new StreamedResponse(fn () => $this->twig->display($attribute->template, $parameters), $status)
: new Response($this->twig->render($attribute->template, $parameters), $status)
? new StreamedResponse(
null !== $attribute->block
? fn () => $this->twig->load($attribute->template)->displayBlock($attribute->block, $parameters)
: fn () => $this->twig->display($attribute->template, $parameters),
$status)
: new Response(
null !== $attribute->block
? $this->twig->load($attribute->template)->renderBlock($attribute->block, $parameters)
: $this->twig->render($attribute->template, $parameters),
$status)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
use Symfony\Bridge\Twig\Tests\Fixtures\TemplateAttributeController;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Twig\Environment;
use Twig\Loader\ArrayLoader;

class TemplateAttributeListenerTest extends TestCase
{
Expand Down Expand Up @@ -65,6 +67,33 @@ public function testAttribute()
$this->assertSame('Bar', $event->getResponse()->getContent());
}

public function testAttributeWithBlock()
{
$twig = new Environment(new ArrayLoader([
'foo.html.twig' => 'ERROR {% block bar %}FOOBAR{% endblock %}',
]));

$request = new Request();
$kernel = $this->createMock(HttpKernelInterface::class);
$controllerArgumentsEvent = new ControllerArgumentsEvent($kernel, [new TemplateAttributeController(), 'foo'], ['Bar'], $request, null);
$listener = new TemplateAttributeListener($twig);

$request->attributes->set('_template', new Template('foo.html.twig', [], false, 'bar'));
$event = new ViewEvent($kernel, $request, HttpKernelInterface::MAIN_REQUEST, ['foo' => 'bar'], $controllerArgumentsEvent);
$listener->onKernelView($event);
$this->assertSame('FOOBAR', $event->getResponse()->getContent());

$request->attributes->set('_template', new Template('foo.html.twig', [], true, 'bar'));
$event = new ViewEvent($kernel, $request, HttpKernelInterface::MAIN_REQUEST, ['foo' => 'bar'], $controllerArgumentsEvent);
$listener->onKernelView($event);
$this->assertInstanceOf(StreamedResponse::class, $event->getResponse());

$request->attributes->set('_template', new Template('foo.html.twig', [], false, 'not_a_block'));
$event = new ViewEvent($kernel, $request, HttpKernelInterface::MAIN_REQUEST, ['foo' => 'bar'], $controllerArgumentsEvent);
$this->expectExceptionMessage('Block "not_a_block" on template "foo.html.twig" does not exist in "foo.html.twig".');
$listener->onKernelView($event);
}

public function testForm()
{
$request = new Request();
Expand Down
Loading