15

I using php7.3 on my project and use Twig in this project

After upgrade my php version to 7.4 i have some error in twig rendering.

I set some html class by Twig variable

For example:

<body class='{{global.direction}} preload {{bodyclass}}'></body>

When using php 7.3 the output of Twig render is:

<body class='ltr preload main'></body>

And no problem in my source.

But when my php upgraded to php 7.4 the output was changed!

<body class='ltr preloadmain'></body>

Twig removed on space before variable and very error was happening in my source :/

Everywhere call variable from Twig, The Twig remove all space before it!

Any solution?

How to fix it?

Biqarar
  • 195
  • 1
  • 6
  • 1
    I don't see why the space would be removed, are you sure you have a space between your preload class and {{bodyclass}} ? – Dylan KAS Dec 09 '19 at 12:10
  • I also made a simple test case and things worked as expected. Though I did start in 7.4 and did not upgrade. It is possible that the great and powerful command of last resort "bin/console cache"clear" might help. – Cerad Dec 09 '19 at 13:53
  • 2
    I had the same problem with twig v1.18.2 and updating to v1.42.4 fixed it for me. https://github.com/twigphp/Twig/pull/3004/commits/1fb0f9701d8443083495cd2645e8a0c45d54c34d seems to be the fix – Simo Heinonen Dec 10 '19 at 16:53
  • 2
    this is cost me a lot-off search and work to add spaces in opencart, am try update opencart Twig but noting happing, if someone have update of opencart Twig please post as answer – Ahmad Odeh Jun 21 '20 at 12:18

5 Answers5

11

The general answer that worked for me (Opencart Twig too) is related to the fix Simo Heinonen mentioned in the comments i.e. https://github.com/twigphp/Twig/pull/3004/commits/1fb0f9701d8443083495cd2645e8a0c45d54c34d

Different versions will have somewhat different code than shown above. For Opencart Twig (most versions but tested with oc 3.0.3.3) what apparently fixed it was :

Find in file Lexer.php function lexData line 163 change:

if (isset($this->positions[2][$this->position][0]) ) {
    $text = rtrim($text);
}

to

if (isset($this->positions[2][$this->position][0]) && ($this->options['whitespace_trim'] === $this->positions[2][$this->position][0])) {
   $text = rtrim($text);
}
user3134164
  • 171
  • 2
  • 4
1

Twig has a problem with PHP 7.4. you can change PHP version or upgrade twig to latest version

jay padaliya
  • 624
  • 6
  • 12
  • A reference would be a great addition to your _comment_. – VisioN Aug 19 '20 at 16:44
  • 1
    @VisioN This is the only easiest solution for this problem. I have the same problem and fix it by just changing PHP version 7.4 to 7.3 – jay padaliya Aug 19 '20 at 16:54
  • What solution? If you make the statement in your _answer_ that Twig has a problem with PHP 7.4, you need to give a reference to what can prove your words. Otherwise, this is more like a comment and not an answer. – VisioN Aug 19 '20 at 17:01
0

We just released Craft 2.8.0, which includes Twig 1.42.4 with a couple PHP 7.4 fixes.

Note that this update also bumps Craft’s minimum PHP requirement up to 5.5.

Brandon Kelly
  • 2,033
  • 2
  • 21
  • 38
0

Same problem here wit an old project using Twig 1.x with no options to update. So far, we have solved the problem inserting the html code for spaces where necessary in the templates. I mean:

<body class='{{global.direction}} preload &#32; {{bodyclass}}'></body>

jap1968
  • 7,747
  • 1
  • 30
  • 37
-2

I use twig in my symfony project and I cannot reproduce the behaviour your describing.

As an (ugly) fix maybe you could change to :

<body class="{{ global.direction ~ ' preload ' ~ bodyclass }}"></body>
Pierrick Rambaud
  • 1,726
  • 1
  • 20
  • 47