Forum

Thread tagged as: Discussion

Template Filters and Performance

I'm really curious if there are any performance considerations when using template filters. For example, does this create any extra calls to the database, etc.? Is it something I should carefully consider, or should I use them freely?

Should I consider any performance difference between filterAfterProcessing and filterBeforeProcessing?

I tend to use them for tiny little utilities such as removing the trailing slash from catPath like this:

/* GROUP REMOVE TRAILING SLASH
=================================================== */
class PerchTemplateFilter_remove_trailing_slash extends PerchTemplateFilter 
{
    /* Notes...
        You may want to do this to remove the trailing slash from catPath
        - e.g. <a href="/<perch:category id="catPath" filter="remove-trailing-slash"/>">
    */
    public function filterAfterProcessing($value, $valueIsMarkup = false)
    {
        // Only remove the character if it's a /
        $value = rtrim($value, '/');
        return $value;
    }
}
PerchSystem::register_template_filter('remove-trailing-slash', 'PerchTemplateFilter_remove_trailing_slash');
Jay George

Jay George 2 points

  • 3 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

There's no significant performance impact of filters themselves. The only performance consideration is what you do inside your filter code.

The impact of something like rtrim() would be so small as to not be reliably measurable.

Thanks. That helps. Should I consider any performance difference between filterAfterProcessing and filterBeforeProcessing? If it doesn’t make a difference to the function

Drew McLellan

Drew McLellan 2638 points
Perch Support

No. Unless you're doing something that is in itself very slow then you don't need to worry about performance here.