Olivier Chretien Olivier Chretien - 19 days ago 5
Twig Question

unknow function error when calling twig extension

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!

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