I have many Twig templates containing many levels of blocks that use tabs for indentation. The final result is a HTML file with lots of lines (3700+) containing up to 10 tabs at their beginning. After having removed the tabs manually, the file is 25 % smaller. I know that I can use the {% spaceless %}
function to wrap this code and remove all the necessary tabs/spaces. I also know that there is other solutions, like separating code in multiple files then include them, but this requires more work.
My question is about performance, removing these spaces with {% spaceless %}
will have a cost, but I'm pretty sure that the operation will be done only once, when the Twig compiler will transform the Twig files in PHP files in the cache. So it should have no negative impact on the prod website. My development environment is too slow to allow me to compare the presence of {% spaceless %}
. Can you please confirm me that using {% spaceless %}
won't slow my website?
Using spaceless will have a negative performance impact on your template rendering performance and it's pretty easy to see why when you look at it's compile
method implementation: Spaceless Node Twig
So, instead of just echoing the output, your template will have to start output buffering, then print node content, and finally do preg_replace on buffered content to remove spaces between html tags.
So all in all, it will have negative impact, but I'm really not sure how big will it be. However, you can create some preg_replace tests to see if it will make you any significant difference.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With