Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spaceless block inside autoescape throws SyntaxException in Twig 3.*

Tags:

php

twig

Just updated from Twig 2.* to Twig 3.*

Most of my templates are wrapped in the {% autoescape %} ... {% endautoescape %} block

And some templates have {% spaceless %} ... {% endspaceless %} block inside.

In final it looks like this:

{% autoescape %}
  ...
  {% spaceless %}
    ...
  {% endspaceless %}
  ...
{% endautoescape %}

And now (after the update to 3.*) I get SyntaxException from Twig.

How should these block be rewritten/supported?

Why such construction is no longer supported by the Twig?

Fatal error: Uncaught Twig\Error\SyntaxError: Unexpected "spaceless" tag (expecting closing tag for the "autoescape" tag defined near line 2). in

like image 426
Sergej Avatar asked Jan 03 '20 10:01

Sergej


1 Answers

You need to use the apply tag now, e.g.

{% apply spaceless %}
 ...
{% endapply %}

reference


The reason is for this construction is to have a consistent codebase as you now can "apply" filter(s) to chunk of texts, e.g.

{% apply upper %}
    This {{ text }} becomes uppercase
{% endapply %}

whereas you needed to concat it in the past and add parentheses

{{ ('This '~text~' becomes uppercase')|upper }}


TLDR To be consistent they've decided to remove the tag spaceless and to introduce the filter spaceless

like image 53
DarkBee Avatar answered Nov 07 '22 20:11

DarkBee