I have these two functions:
function is_expired($object) { if (new DateTime('NOW') > $object) { return true; } else { return false; } } function is_not_started($object) { if (new DateTime('NOW') < $object) { return false; } else { return true; } } $object is Datetime object fetched from db. and I also registered functions as twig filter.
For a voting system I have two fields start_date and expiry_date, I want to see if this is not expired but already started.
In twig I have:
{% if (category.endVote|is_expired == false) or (category.startVote|is_not_started == true) %} <td><a href="voting.php?id={{ category.id }}">{{ category.categoryName|e }}</a></td> {% else %} <td>{{ category.categoryName|e }}</td> {% endif %} I mean I want to see categoryName as link only if this is started and is NOT expired. but it doesn't work as expected. What wrong I did in my if clause?
if (...)or(...). You've basically described the solution yourself :)is_started, then negate it inside logical expressions. When you name it negatively, to get a positive version, you have to negate the negation... so you end up with an expression that reads as "if not is not started...", which isn't really natural. It's much cleaner to just have "if is started" and "if not is started".is_not_startedtois_startedmakes your code easier to understand. Having a double negative likeis_not_startedreturning false can easily become hard to wrap your head around.is_active? Or even better add a methodisActive()in your object?