Skip to content
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

Add a way to stream template rendering #4504

Merged
merged 1 commit into from
Dec 18, 2024
Merged

Conversation

fabpot
Copy link
Contributor

@fabpot fabpot commented Dec 16, 2024

Even if we're using yield internally, there is no easy way to stream template rendering. This new method can be used like this to HTTP stream a template with Symfony:

use Symfony\Component\HttpFoundation\StreamedResponse;
use Twig\Environment;

$twig = new Environment(/* ... */);

$response = new StreamedResponse($twig->load('index')->stream([]));
$response->send();

@fabpot
Copy link
Contributor Author

fabpot commented Dec 16, 2024

I thought about streaming blocks as well, but I'm not sure this is worth it.

@yceruto
Copy link
Contributor

yceruto commented Dec 16, 2024

This will allow us to improve this method https://github.com/symfony/symfony/blob/7.3/src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php#L267 👍

I thought about streaming blocks as well, but I'm not sure this is worth it.

I think yes, since there is a renderBlock() method added recently https://github.com/symfony/symfony/blob/dd882dbbd4910dce577d5bd280920e259dd782a5/src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php#L428

Copy link
Contributor

@GromNaN GromNaN left a comment

Choose a reason for hiding this comment

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

Will you add a helper Twig\Environment::stream() as well?

src/TemplateWrapper.php Outdated Show resolved Hide resolved
@smnandre
Copy link
Contributor

I think yes, since there is a renderBlock() method added recentl

And it's used via the #[Template] attribute too.

@smnandre
Copy link
Contributor

And this will probably allow some decent optimizations in the TwigEngineRenderer / block systems :)

@fabpot
Copy link
Contributor Author

fabpot commented Dec 18, 2024

I've just added a streamBlock() method as well.

@fabpot
Copy link
Contributor Author

fabpot commented Dec 18, 2024

Will you add a helper Twig\Environment::stream() as well?

No, I don't think this is needed. The Environment class has already too many methods. I would like to do the opposite actually and deprecate the Environment::render() method.

@fabpot fabpot merged commit f99eab8 into twigphp:3.x Dec 18, 2024
49 of 50 checks passed
@fabpot fabpot deleted the stream-template branch December 18, 2024 18:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants