Menu Close
    Hire us Request quote Reading list Switch to dark mode
    lockdownWebkulUp

    Twig – How to create custom extensions or filters

    In PHP template engine, Twig there are various inbuilt awesome filters using that anyone can write large apps in less no. of lines like


    and various others filter too but these are limited and various default PHP function are not available in Twig then how can we use those ? For those cases Twig provide ways so that you can add your custom filters/ extension. I am going to show you how.

    First  – Create your custom extension class extending class \Twig_Extension 


    It’s basic requirement is to declare two functions getFilters() and getName()  so we add those here. In getFilters() function we declare our all custom filters/ extensions and in getName() function we add this class name (any name).

    . . .
    Subscribe Articles

    Just enter your email below to subscribe and receive weekly updates about the most interesting similar articles.

    You need to accept conditions to receive letters.
    You've Subscribed!

    Share article via

    . . .


    So we created a class with various custom functions now we have to just notify Symfony about these filters/ extensions. We do it after declaring this in our service.


    That’s it our Custom filters/ extensions are ready to use. We can use these like Twig default once.


    Framework used – Symfony 2.7

    . . .

    Comment

    Add Your Comment

    Be the first to comment.

    Back to Top
    css.php
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again
    Close