Skip to content
Advertisement

How to set if clause in twig?

I have these two functions:

JavaScript

$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:

JavaScript

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?

Advertisement

Answer

This is what I would recommend.

JavaScript

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.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement