2

I need to set some conditions in twig, so i have this:

{% if app.session.get('campaignVersion') is not null and is not '4.4d'}
...
{% elseif app.session.get('campaignVersion') is null or '4.4d' %}
...
{% endif %} 

But i have errors with syntax and logic, maybe it must have an standard operator such as !=, what i'm doing wrong? Thx for help.

1
  • 1
    BTW, I would make such computation in the controller, leaving you with a more reusable template in this case.
    – moonwave99
    Commented Apr 9, 2014 at 10:30

2 Answers 2

6

Twig is not a human language interpreter :-)

Twig is not able to implicitly know who is the subject in and is not '4.4d'.

Try with:

{% if app.session.get('campaignVersion') is not null and app.session.get('campaignVersion') != '4.4d' %}

Or for better readibility:

{% if app.session.get('campaignVersion') not in [null, '4.4d'] %}
2
  • Unexpected token "string" of value "4.4d" ("name" expected)
    – Lukas
    Commented Apr 9, 2014 at 12:03
  • Edited the first example. "is not" takes a test (who begins with a name token), not a string. I replaced it by !=. Anyway, you should look at the second example.
    – Alain
    Commented Apr 9, 2014 at 15:48
0

A % is missing at the end of the first line

2
  • OK, this was a problem, but also i have some problems with logic, or conditions definition...
    – Lukas
    Commented Apr 9, 2014 at 10:49
  • I think they are a problem in the second condition of the elseif you said is not null (this is ok) then you just put '4.4d' without saying if: it is or it is not
    – Ryokel
    Commented Apr 9, 2014 at 11:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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