2
votes

I'm looking to create a twig extension but Symfony keeps telling me my function is unknown.

Here is my class:

<?php

namespace AppBundle\Twig\Extension;

use Twig_Extension;
use Twig_SimpleFunction;

class FormExtension extends Twig_Extension
{
    public function getFonctions()
    {
        $twigClass = 'Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode';
        $options = array(
            'node_class' => $twigClass,
            'is_safe' => ['html']
        );

        return array(
            'form_color' => new Twig_SimpleFunction($this, null, $options)
        );
    }

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

And here is the service declaration in AppBundle\Resources\config\services.yml

services:
    app.form_extension:
        class: AppBundle\Twig\Extension\FormExtension
        tags:
            - { name: twig.extension }

I'm probably missing somehing but I can't find out what. Please help!

2

2 Answers

3
votes
  1. Correct getFonctions to getFunctions
  2. Try this way:
    ...
        return array(
            new Twig_SimpleFunction('form_color', null, $options)
        );
    ...
0
votes

For me same error was when I added my function to getFilters() instead of getFunctions()