PHP 7.4 trimming whitespace between string variables
Asked Answered
M

1

9

I'm running into an issue where when I have multiple variables being output into a class attribute, they all get compressed into a single word rather than kept as separate words as originally authored, as in

<div class="className {{ variable1 }} {{ variable2 }}"><div>

At runtime this is being rendered as:

<div class="className variable1variable2"><div>

Any idea what would cause this and how to avoid it?

Mcglone answered 5/5, 2020 at 9:5 Comment(5)
This output is weird, since there is a space between the {{}} {{}}, it should work as expected. Try to use string concate instead, such as {{ variable1 ~ ' ' ~ variable2 }}Pellikka
Does WordPress’s the_content filter run over this?Rosati
This runs outside the scope of the_content. Trying to pinpoint what triggers it as apparently it doesn't affect all sites running PHP 7.4 and Timber.Mcglone
I don't think this is caused by PHP or Twig specifically - could you expand your question with more information about your environment? Ie, any framework, plugins, type of server etc. My guess is that there's a filter somewhere trying and failing to be helpful.Georganngeorge
You may be onto something @Bananaapple, as I don't get consistent behaviour across all sites. Trying to pinpoint the specific set of circumstances that triggers this.Mcglone
H
8

Faced the same issue on PHP 7.4. Found this fix on GitHub.

So if you're using Twig 1, upgrade it at least to v1.43.0.

If you're using Twig 2, upgrade it at least to v2.10.0. (my case - I had v2.7.4 in the project).

Huckaby answered 1/8, 2020 at 4:37 Comment(2)
Anyone know what change in PHP 7.4 triggered this? The changelog for the fix seems highly unspecific.Ferriage
Relevant solution for 1.2x and similar in case someone can't upgrade: https://mcmap.net/q/736764/-twig-problem-with-php-7-4-in-write-variablePlaque

© 2022 - 2024 — McMap. All rights reserved.