Forum

Thread tagged as: Question

How do I run a function inside a Template filter?

I'm trying to create a filter that returns whether a date is a weekend or not. I can get this working in plain ol' PHP but I suspect I am getting a blank page with the filter because I'm trying to run a function within a class.

I'd be really grateful if anyone can suggest a way around this. It seems a PHP question rather than a Perch question.

class PerchTemplateFilter_weekend_or_weekday extends PerchTemplateFilter 
{
    public function filterBeforeProcessing($value, $valueIsMarkup = false)
    {
        function isWeekend($date) {
            return (date('N', strtotime($date)) >= 6);
        }

        if (isWeekend($value)) {
            $value = "weekend";
        } else {
            $value = "weekday";
        }

        return ($value);
    }
}
Jay George

Jay George 2 points

  • 4 years ago

I solved this by breaking out the function. Here is the solution.

class PerchTemplateFilter_weekend_or_weekday extends PerchTemplateFilter 
{
    /* Notes...
        - Should be used without a format attribute
        - e.g. <perch:content id="event_date_start" type="date" filter="weekend-or-weekday"/>
    */
    public function filterBeforeProcessing($value, $valueIsMarkup = false)
    {
        if ((date('N', strtotime($value)) >= 6)) {
            $value = "it's the weekend";
        } else {
            $value = "it's a week day";
        }

        return ($value);
    }
}