This seems like a very basic question but I assure you I've run the gamut of solutions for this to work and I still haven't managed to find a solution.
The problem is this:
A twig value will be set with a value of 1, 0, or null by a select box and the value will then be used to set the selected value for that box.
There are two filters that are chosen - 1 for active, 0 for inactive.
The twig code in question is as follows:
<option value="null">Select an Option</option>
<option value="1"{% if filterStatus == 1 %} selected{% endif %}>Active</option>
<option value="0"{% if filterStatus == 0 %} selected{% endif %}>Inactive</option>
Is what I expected to use. Below is one of many variations I attempted:
{% if filterStatus == 0 and not filterStatus != 'null' %}
I just can't seem to ensure the value is 0.
Also don't be fooled by the "null" value in the option value attribute. This is used in routing, but translates to a literal NULL in the system and not a string by the time it makes it to twig.
Any help is greatly appreciated.
By setting strict_variables to false If set to false , Twig will silently ignore invalid variables (variables and or attributes/methods that do not exist) and replace them with a null value. When set to true , Twig throws an exception instead (default to false ).
Twig != is a comparison operator. You can use != to compare any two things that php lets you compare, and what you get back is a boolean.
The way of checking for not null is:
{% if var is not null %}
But you can use the same as
function:
{% if var is same as(0) %}
{# do something %}
{% endif %}
Ref: http://twig.sensiolabs.org/doc/tests/sameas.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With