1

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?

6
  • Notice the word you use in your description: "if this is started and is NOT expired" and the one in your code: if (...) or (...). You've basically described the solution yourself :) Commented May 7, 2021 at 9:25
  • actually I tried that too, but I guess something still wrong in functions, can you please look at them and help? Commented May 7, 2021 at 9:27
  • 1
    A tip: don't write functions that handle "negative" cases. Make a function 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". Commented May 7, 2021 at 9:32
  • 1
    Just a small note. Renaming your function is_not_started to is_started makes your code easier to understand. Having a double negative like is_not_started returning false can easily become hard to wrap your head around. Commented May 7, 2021 at 9:33
  • 1
    Why don't you just combine them in a filter is_active? Or even better add a method isActive() in your object? Commented May 7, 2021 at 9:56

1 Answer 1

1

This is what I would recommend.

function can_vote($object) { $now = new \DateTime('now'); return $object->getEndVote() < $now && $object->getStartVote() > $now; } {% if category|can_vote is same as(true) %} <td><a href="voting.php?id={{ category.id }}">{{ category.categoryName|e }}</a></td> {% else %} <td>{{ category.categoryName|e }}</td> {% endif %} 

Instead of two separate functions, merge these into one and pass in the entire category object. Also in the twig part, I recommend to use the more stricter equation instead of "==" the one in my code equates to "===". Make note of the getEndVote() and getStartVote() getter methods, if You use public properties, change those to reflect the correct names.

If you wish to use two separate methods in the end it is not difficult to extract that functionality but i think this is a better solution.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.