Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9239067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:54:08+00:00 2026-06-18T07:54:08+00:00

I have a twig based project with Symfony 2. Due to Symfony 2 nature,

  • 0

I have a twig based project with Symfony 2. Due to Symfony 2 nature, Namespaces are used. Because of this i am unable to provide global functions outside of namespace.

This is my Twig Extension class:

<?php
namespace Web\MailBundle\Twig;

use Twig_Extension, Twig_SimpleFilter;

class Twig extends Twig_Extension
{
    public function getName()
    {
        return 'twig_extension';
    }

    public function getFilters() {
        return array(
            'shortKey'  => new Twig_SimpleFilter('shortKey', 'myCustomFilterFunction')
        );
    }

    public function myCustomFilterFunction() {
        //code here...
    }

Result:
FatalErrorException: Error: Call to undefined function myCustomFilterFunction()

Why:
Because, Twig is trying to find this function but it is inside of a class. If i move it to outside of class this time i face with namespaces. Because it is namespaced.

Research:
I digg the codes. Twig do the same thing. They write the filters and functions outside of class. But due to namespace i canno do that. If possible i wanto to do that by using proper solution. If it fails; I will came with the latest solution which is creating another php file without namespace and include it to the project…

—

How can i get over it? Twig Filter has been deprecated and we have to use SimpleFilter method. But i just couldn’t get it done.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T07:54:09+00:00Added an answer on June 18, 2026 at 7:54 am

    You can pass a array-based callable as the second parameter to the Twig_SimpleFilter constructor – in your case, use an array like so:

    public function getFilters() {
        return array(
            'shortKey'  => new Twig_SimpleFilter('shortKey', array($this, 'myCustomFilterFunction'))
        );
    }
    

    which will use your object’s method when the filter is used.

    See the examples in the Twig documentation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Twig extension in my Symfony project which renders a twitter widget.
I have been looking at this twig code for a while, and tried a
I have a html.twig file with this {% set num= 1%} {% for idea
I have Symfony framework with TWIG template engine. I am making 1000 page pdf
In my symfony 2 project I have a bundle at src/Cinergy/Bundle/PeopleServiceBundle Now I'd like
I want to override the base tempale to be used in twig. I have
I have this custom form field {# src/Acme/DemoBundle/Resources/views/Form/fields.html.twig #} {% block gender_widget %} {%
I have a problem with twig module for kohana3 (this one https://github.com/jheathco/kohana-twig ). After
I'm new to symfony and twig and I have some headache with security, firewalls
Im using Symfony and Twig in a Silex application. I have a registration page

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.