Twig icon indicating copy to clipboard operation
Twig copied to clipboard

If statement with nothing set inside throws error

Open johnwbaxter opened this issue 3 years ago • 3 comments

Create an "if" statement with nothing set inside throws error if this is done outside the {% block content %} tag.

{% extends "_layouts/_layout.twig" %}

  {% if craft.app.request.segment(2) != "" %}
  
    {% else %}
       {%  set foo = "bar" %}
  {% endif %}

{% block content %}
 {# IT DOESN'T MATTER WHAT'S IN HERE #}
{%endblock %}

or without the {% else %}

{% extends "_layouts/_layout.twig" %}

{% if craft.app.request.segment(2) != "" %}

{% endif %}


{% block content %}
   {# IT DOESN'T MATTER WHAT'S IN HERE #}
{%endblock %}

johnwbaxter avatar Jun 14 '22 08:06 johnwbaxter

Debugging this, it seems to be caused by the removal of whitespace-only text nodes that are at the root of child template. This breaks the structure of the IfNode tests. We might want to replace them with an empty Node instead of removing them entirely.

stof avatar Jun 14 '22 08:06 stof

Anyone willing to work on a fix?

fabpot avatar Aug 11 '22 07:08 fabpot

I'm gonna give it a try.

xabbuh avatar Aug 11 '22 07:08 xabbuh

I've taken over the PR where I took a different approach to fix the bug.

fabpot avatar Dec 26 '22 17:12 fabpot