Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twig problem with php 7.4 in write variable

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?

like image 1000
Biqarar Avatar asked Dec 09 '19 12:12

Biqarar


People also ask

What is Twig template engine in PHP?

Twig tutorial shows how to use Twig template engine in PHP applications to generate documents. Twig is a PHP template engine. It was created by Symfony developers. Twig files have the extension of .html.twig; they are a mix of static data such as HTML and Twig constructs.

What is a global variable in twig?

The Twig documentation covers this. A global variable is like any other template variable, except that it's available in all templates and macros: Thanks. I'm trying to understand this command. Does it define a global twig variable called 'themename' with contents of a previous defined php variable called $variation?

What is twig syntax?

Twig syntax consists of tags, filters, functions, operators, and tests. First, we set up Twig. We install Twig with composer. We will place our template files into the template directory. We need to add the autoload.php file to our scripts.

How to prevent the inclusion of unknown JS file in twig?

The inclusion of unknown JS file can be prevented with autoescaping. If autoescaping is enabled, we can show the raw input with the raw filter. This partial output shows how the characters are escaped. Twig tests allow to test data. The tests are applied with the is operator.


2 Answers

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);
}
like image 162
user3134164 Avatar answered Oct 22 '22 08:10

user3134164


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

like image 21
jay padaliya Avatar answered Oct 22 '22 10:10

jay padaliya