Forum

Thread tagged as: Problem

Conditional output in templates based on date?

I'm trying to customise search results at the template stage. What I'd like to achieve is for the result to only appear if a date field in the result is less than or equal to the current date.

I'm passing in the current date from search.php like this:

PerchSystem::set_var('current-date', date('Y-m-d'));

And I'm using Perch:if statements to try and control the display of the results like this (where item_post_date is a date formatted in Y-m-d):

<perch:if id="item_post_date"  match="lte" value="current-date">
    ... item output code here
<perch:else>
    <p>Item not yet published
</perch:if>

The problem is, the item is always displayed. Is it actually possible to compare these two variables in the template this way, or do I need to use an alternative date format?

Mike Armstrong

Mike Armstrong 0 points

  • 2 years ago
Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

Hello Mike,

You need to wrap value in braces:

<perch:if id="item_post_date" match="lte" value="{current-date}">

That's it! Thank you so much.