Twig and Symfony2 - test if a number is even - symfony

I would like to create a table with different color for each even row. For that, I created the css and I will have to check in my twig template if the iteration index is even or not.
{% if loop.index == "even" %}
But how to check if a number is even in twig? Thank you.

Twig has a built in "even" test:
{% if (loop.index is even) %}
...your code here
{% endif %}

You have to use the "modulo" operator as follow :
{% if(loop.index%2 == 0) %}
...your code here
{% endif %}

I think a more cleaner way would be:
{{ loop.index is odd ? 'is-odd' : 'is-even' }}

Related

How would I write multiple "if" conditions in one line in timber/twig?

I have a situation where I would like to write multiple "if" condition in one line such as example: {% if room_status == 'For Rent' and not 'Booked' %} this is obviously not working but you get the picture of what I am trying to accomplish.
But have no idea how to write this other that doing this:
{% if room_status == 'For Rent'%}
{% if room_status != 'Booked' %}
{# <Then run some code here> #}
{% endif %}
{% endif %}
Any suggestions greatly appreciated!
From the Twig If Documentation:
For multiple conditions, and and or can be used:
{% if temperature > 18 and temperature < 27 %}
<p>It's a nice day for a walk in the park.</p>
{% endif %}
So, in your case this should work:
{% if room_status == 'For Rent' and room_status != 'Booked' %}
{# <Then run some code here> #}
{% endif %}
Although it seems to me that you don't really need the second check. The room_status variable can only contain one value, right? It can't be both 'For Rent' and 'Booked' at the same time, it's either one or the other.

Twig - loop default value

I am trying to print array from the cotroller into the twig temlate. I want to print "-" whenever array is NULL. My problem is that in for-loop case it writes nothing, however single row working fine. Is there some simple way how to do it correctly?
this is not working as i expected
{% for key in keywords|default('-') %}
{{ key~', '}}
{% endfor %}
this is working
{{ key |default('-')}}
You can use an {% else %} construct on a for loop to do something else if the array is null:
{% for key in keywords %}
{{ key~', '}}
{% else %}
-
{% endfor %}
See the documentation here.

Getting current route twig

I want to get the current route on twig , I used this two code but It fail always
Code 1:
{% if app.request.get('_route') == 'my_route' %}
//Do something
{% endif %}
Code 2:
{% if app.request.attributes.get == 'my_route' %}
//Do something
{% endif %}
Use the "app_dev.php" at the end of your URL to debug and check what route is being used at the bottom. For example I show "route1" here:
Then in your twig template you can use something like this:
{% if app.request.attributes.get('_route') == 'route1' %}
<h1>test</h1>
{% endif %}
I verified this works. I'm using Symfony3 though.
Maybe also try instead of "app.request.attributes.get()", try these:
app.request.pathinfo
app.request.uri
See if those work.
Also if the route is indeed null, try:
{% if app.request.attributes.get('_route') == '' %}
<h1>test</h1>
{% endif %}

Twig: filter in an if condition

I want to use an filter in an if condition in Twig. The reason for this is a Symfony2 attribute, which I can't compare directly, I have to change it beforehand. I have started with this code:
{% if app.request.attributes.get('_controller')|split('::')|first == 'some\controller\name' %}
do something
{% endif %}
Unfortunately this does not function. So I thought I would use set before the comparison:
{% set controller = app.request.attributes.get('_controller')|split('::')|first %}
{% if controller == 'some\controller\name' %}
do something
{% endif %}
{{ controller }} {# would print 'some\controller\name' #}
Guess what? "do something" is not printed, even if the variable controller now exists and has the value I compare it with. What am I doing wrong?
Ok I tested it, Twig has a strange behavior. "\" is escaped or something like this.
I extended my twig environement with the var_dump function, check this:
{{ var_dump("Sybio\Bundle\WebsiteBundle\Controller\MainController") }}
//string(48) "SybioBundleWebsiteBundleControllerMainController"
{{ var_dump(app.request.attributes.get('_controller')|split('::')|first) }}
// string(52) "Sybio\Bundle\WebsiteBundle\Controller\MainController"
{{ var_dump("Sybio\\Bundle\\WebsiteBundle\\Controller\\MainController") }}
// string(52) "Sybio\Bundle\WebsiteBundle\Controller\MainController"
That's why your test is always false.
You need to double the backslashes of your compared string...
{% if app.request.attributes.get('_controller')|split('::')|first == 'some\\controller\\name' %}
do something
{% endif %}

Twig and variables as numbers

I'm getting a number from database (let's say, 10) and I want to use it in if/else statement. Please note, that my variable may be equal to 0 or null.
My variable is item.getRate.getRate. I'm showing it in template like this:
{{ item.getRate.getRate | default('0') }}
I try try to do a if statement by doing
{% if item.getRate.getRate == 1 %}something{% endif %}
but it doesn't work.
It is runned in a loop, and one of the items has empty getRate. Could this be the problem? If yes - how can I avoid it?
You can combine both tests to make your code easy to read.
{% if item.getRate.getRate is defined and item.getRate.getRate == 1 %}
checked="checked"
{% endif %}
I just had to check if variable exists ;)
{% if item.getRate.getRate is defined %}{% if item.getRate.getRate ==
1 %} checked="checked"{% endif %}{% endif %}
A little long, but works. Anyone had a better idea? 'Cause it would be great, now it's kinda ugly :))
Assuming getRate having a non-zero, non-null value is necessary for this indication of boolean value, as your example suggests:
{% if not(not(item.getRate.getRate)) %}
checked="checked"
{% endif %}

Resources