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 1044391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:49:17+00:00 2026-05-16T15:49:17+00:00

Currently I place my function in a class and pass an instance of this

  • 0

Currently I place my function in a class and pass an instance of this class into template and call my required function as a class method.

{{ unneededclass.blah() }}

I need to do like below

{{ blah() }}

Is it possible?

  • 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-05-16T15:49:18+00:00Added an answer on May 16, 2026 at 3:49 pm

    Update 5/14/2015

    Commenters point out that I’m mostly wrong. If you really need a function, and not a filter or macro, you can do it as suggested in the Twig docs:

    $twig = new Twig_Environment($loader);
    $function = new Twig_SimpleFunction('blah', function () {
       // ...
    });
    $twig->addFunction($function);
    

    And use like

    {{ blah() }}
    

    In short, no, this is not possible.

    However, hope is not lost!

    Filters

    If this function blah() of yours is meant to modify an existing variable, then it is a filter.

    An example:

    //in your PHP
    function format_date($date_string,$format_string) {
        return date($format_string,strtotime($date_string));
    }
    $twig_env->addFilter('format_date',new Twig_Filter_Function('format_date'));
    
    {# in your template #}
    {{ some_date|format_date('n/j/Y') }}
    

    (The first argument is the variable you are filtering, the second is supplied by normal means)

    Macros

    If, as you have indicated above, your function simply outputs HTML, then it is a good candidate for a macro.

    An example:

    {# in your template #}
    {% macro say_hello() %}
    <p>Oh! Hello, world!</p>
    {% endmacro %}
    
    {# ... later on ... #}
    {{ _self.say_hello() }}
    

    Or with parameters:

    {% macro input(name,value,type) %}
    <input type="{{ type|default('text') }}" name="{{ name }}" value="{{ value }}">
    {% endmacro %}
    
    {{ _self.input('phone_number','867-5309') }}
    {{ _self.input('subscribe','yes','checkbox') }}
    

    Why?

    The thing to remember is that Twig templates represent a view, in terms of MVC. This means they are isolated in terms of their environment, and can only represent the context you pass them via the data array you pass in the $template->render() method.

    This is a good thing, as it decouples your presentation from your logic and data. If you can arbitrarily call functions, then you suddenly increase that coupling, which is a bad thing.

    The other reason for this is the way PHP handles callbacks. Think about how you would have to pass that function into your template… Probably something like this:

    function blah() {
        return "<p>Oh! Hello, world!</p>";
    }
    
    $template = $twig_env->loadTemplate('template.html');
    echo $template->render(array('blah'=>'blah'));
    

    In your template, the context variable blah is now just a string containing 'blah'.

    In vanilla PHP, when you use variable functions like this (try to use a string variable like a function), it (more or less) performs a lookup for that function, then calls it. You are not passing the function, just it’s name.

    The thing is, you cannot possibly pass a function into a template, because PHP’s only mechanism for doing this is by name-string, and once inside a template, that name is no longer a function name and just a string.

    A little bit long winded, but I hope that helps!

    If you want more documentation, the official docs are here.

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

Sidebar

Related Questions

Currently I have a form class Project extends AbstractType { public function buildForm(FormBuilder $builder,
I'm currently in a class on systems software development. We are writing the two-pass
I'm currently learning scala. Why this code doesn't work: class GenClass[T](var d : T)
My place of work currently uses CVS. A git migration is planned but it
Currently i have a build process in place for all of our apps using
I'm currently researching a project for the place that I work in. We are
I'am currently working on GWTs Activity-Place implementation. Now I have found problem with the
The margin/padding is taking too much place in the app. Currently the layout height
I currently have a scrolling anchor animation that also adds an active class to
I am currently migrating my code from the old-style enum to enum class. The

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.