Programming Tools and Technologies

How to Create Custom Filter in Twig

Twig is the Fast, Secure, Flexible, and Powerful template engine of PHP. It has lot’s of great features for develope the front end. It’s has tons of built in filters, but sometimes we need to create our own custom filter and use them into Twig. With this article I am trying to describe you how to create custom filter in Twig.

I am showing an example so you can understand better. Go, where your Twig in loading paste this code.

Here, the custom filter name is ‘convert_object_to_array‘ Which is responsible for convert an object to array. Now you can call that filter in your twig file by {{ objectData | convert_object_to_array }}. Now you can type casting your object into array with this custom filter.