Menu Close

How to fix TWIG whitespace bug in PHP 7.4 and Opencart

If you recently upgraded to php 7.4 and are using Opencart 3.0.x.x, you may encounter some bugs. In the following description I will show you how to repair them. If you login to the admin pageĀ  and Display errors option is Enabled, then on your screen you will see more messages with this error:

Notice: Trying to access array offset on value of type null in /storage/vendor/ on line 1753

To fix this error/warning you need to go to your storage/vendor and edit and change this code:

$key = $key[1];

in to:

$key = isset($key[1]) ? $key[1] : null;

Or to set Display errors to Off.

Another issue is whitespace from TWIG library doesn’t work on php 7.4 as you can see in the screenshot below where icons should be displayed.

To fix this design bug you need to navigate to system/library/template/twig/ and edit the file Lexer.php. Here you need to change this code(lines 163-165 ):

$text = rtrim($text);

with this code

            if ($this->options['whitespace_trim'] === $this->positions[2][$this->position][0]) {
                // whitespace_trim detected ({%-, {{- or {#-)
                $text = rtrim($text);

After that whitespace will work again and icons will be shown.