29

In my template I want to output the server timezone.

My template has something like

{{ getservertimezone }}

Then in the services.yml config for that bundle I have

my.twig.extension:
    class: My\WebsiteBundle\Extensions\Twig\SomeTemplateHelper
    tags:
           - { name: twig.extension }

And my SomeTemplateHelper looks like

namespace My\WebsiteBundle\Extensions\Twig;

class SomeTemplateHelper extends \Twig_Extension
{

    public function getFilters() 
    {
        return array(
            'getservertimezone'  => new \Twig_Filter_Method($this, 'getServerTimeZone'),
        );
    }

    
    public function getServerTimeZone()
    {
        if (date_default_timezone_get()) {
            return date_default_timezone_get();
        } else if (ini_get('date.timezone')) {
            return ini_get('date.timezone');
        } else {
            return false;
        }
    }

    public function getName()
    {
        return 'some_helper';
    }
    
}

But I can't call this method unless it's used like a filter: {{ someval | getservertimezone }}; is there a way to just do a straight {{ getservertimezone() }} call?

4 Answers 4

45

Use getFunctions() instead of getFilters()

public function getFunctions()
{
    return array(
        new \Twig_SimpleFunction('server_time_zone', array($this, 'getServerTimeZone')),
    );
}

Twig filters are used to filter some value.

{{ "some value" | filter_name_here }}

Btw, you can define both filters and functions in the same class.

Sign up to request clarification or add additional context in comments.

1 Comment

Twig_Function_Method is deprecated use Twig_SimpleFunction instead. I open an issue on the symfony doc github.com/symfony/symfony-docs/issues/3275
6

Instead of getFilters, override getFunctions and use Twig_Function_Method instead of Twig_Filter_Method.

Comments

6

In the newer versions of Twig he should be using Twig_SimpleFunction instead of Twig_Function_Method and Twig_SimpleFilter instead of Twig_Filter_Method, since Twig_*_Method are deprecated (I am using Twig v. 1.24.0 along with Symfony 2.8.2)

Comments

2

Symfony ^2.6 - twig ^1.38

See example below


AppExtension.php

namespace Your/NameSpace;

class AppExtension extends \Twig_Extension {

    public function getFilters() 
    {
        return array(
            new \Twig_SimpleFilter('cdn_asset_filter', array($this, 'cdn_asset_filter')),
        );
    }

    public function getFunctions()
    {
        return array(
            new \Twig\TwigFunction('cdn_asset_function', array($this, 'cdn_asset_function')),
        );
    }


    public function cdn_asset_filter($path) 
    {
        return "https://cdn.example.com/$path";
    }


    public function cdn_asset_function($path) 
    {
        return "https://cdn.example.com/$path";
    }
}

view.html.twig

// Filter

<img src="{{ 'path/to/image.png'|cdn_asset_filter }}">

// result : <img src="https://cdn.example.com/path/to/image.png">


// Function

<img src="{{ cdn_asset_function('path/to/image.png') }}">

// result : <img src="https://cdn.example.com/path/to/image.png">

app/config/services.yml

services:
    my_global_filters_and_functions:
        class: Your/NameSpace/AppExtension
        tags:
            - { name: twig.extension }

This is how i used custom functions in Twig in one my old projects, i don't know if it's a best practice, but it worked for me.


Resources : Twig Documentation - Extending Twig

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.