在 Symfony/Twig 中完成渲染之前刷新输出 [英] Flushing output before rendering completed in Symfony/Twig
问题描述
是否可以在渲染完成之前刷新输出缓冲区?我的观点是提高网站的感知性能.
Is it possible to flush output buffer before rendering completed? My point is to improve perceived performance of the website.
例如管理界面中有一个大表(如 5 000 行)要呈现.我想告诉 Twig/Symfony 在 </head>
渲染后立即刷新输出,这样浏览器就可以开始下载样式/javascript,然后每 500 行刷新输出.
E.g. there is a big table (like 5 000 rows) in admin interface to be rendered. I'd like to tell Twig/Symfony to flush output right after </head>
has been rendered, so the browser can start downloading styles/javascript, and then flush output after every 500 rows.
推荐答案
你要使用的是 Twig {% flush %}
命令,它相当于 PHP 函数 flush()
.将它放在模板中 </head>
标签之后,以便将当前缓冲区发送到浏览器.每次调用它时,它都会输出缓冲的任何输出,因此将其放入循环中即可正常工作.
What you want to use is the Twig {% flush %}
command which is the equivalent of the PHP function flush()
. Place it right after the </head>
tag in your template for it to send the current buffer to the browser. Each time you call it it will output any output buffered so putting it in a loop will work fine.
有关详细信息,请参阅文档.
For more info see the docs.
这篇关于在 Symfony/Twig 中完成渲染之前刷新输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!