8

If I write:

{% if entry.section not 'Homepage' %}Blabla {% endif %}

this results in the error:

Unexpected token "operator" of value "not" ("end of statement block" expected)

But if I write:

{% if entry.section != 'Homepage' %}Blabla {% endif %}

It works.

In the twig docs there is no mentioning of != as an operator but it seems to be working just fine. Is != a craft specific operator?

So whats the difference between not and != ?

  • I could be wrong, but I believe 'not' is only used for checking values that are 'false'. != (and ==, etc) are for string comparisons. – Steve Abraham Jan 8 '15 at 13:28
13

Twig != is a comparison operator.

The following comparison operators are supported in any expression: ==, !=, <, >, >=, and <=

You can use != to compare any two things that php lets you compare, and what you get back is a boolean.

Twig not is a logic operator. (so are and and or)

not: Negates a statement.

5

You could write {% if not entry.section == 'Homepage' %}

Use not to perform a negative test, not a string comparison.

0

This should be what you're looking for:

{% if entry.section not same as('Homepage') %}Blabla {% endif %}
  • Note that this will compare the translated name of the section, not the unique handle attribute since the __toString function doesn't return the handle. Of course it is possible but I would not suggest to do this. The better approach would be to check the handle. When you create a new locale your condition might fail due to different translations for Homepage – Robin Schambach Mar 13 '18 at 17:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.