Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot use Twig_Extention when I {{ render }} a Controller

In my template I render the following controller:

{{ render(controller('AppBundle:Widgets:myWidget'))  }}

The WidgetsController as the convention dictates has the following:

namespace AppBundle\Controller;

use AppBundle\Constants\WidgetsConstants;
use AppBundle\Managers\DataFetch\WidgetsFetchingStrategy;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class WidgetsController extends Controller
{
  public function myWidgetAction(){
      return $this->render('widgets/myWidget.html.twig',[
          'images'=>[
            'http://example.com/myarticle'
            'http://example.org/harem',
            'http://example.org/tentacle',
          ],
      ]);
  }
}

And the myWidget.html.twig has the following:

{% for key,url in urls %}
      <img src="{{ censor(url) }}" />
{% endfor %}

And the censor is defined via the following twig plugin:

namespace AppBundle\Twig;

class SanitizeArticlePhotosForList extends \Twig_Extension
{
    public function getFilters()
    {
        return array(
            new \Twig_SimpleFilter('censor', array($this, 'sensorAction')),
        );
    }

    public function sensorAction($photoHtmlTag)
    {
      return str_replace(['tentacle','harem'],'censored',$photoHtmlTag);
    }
}

But I get the following Twig_Error_Syntax error:

Unknown "censor" function.

Do you fellows know why? Over my services.php I have these settings:

use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

// To use as default template
$definition = new Definition();

$definition
->setAutowired(true)
->setAutoconfigured(true)
->setPublic(false);

$this->registerClasses($definition, 'AppBundle\\', '../../src/AppBundle/*', '../../src/AppBundle/{Entity,Repository,Resources,Tests}');

// Changes default config
$definition->setPublic(true)->addTag('controller.service_arguments');

// $this is a reference to the current loader
//Loafing Controllers
$this->registerClasses($definition, 'AppBundle\\Controller\\', '../../src/AppBundle/Controller/*');
$this->registerClasses($definition, 'AppBundle\\Twig\\', '../../src/AppBundle/Twig/*');

So fellows do you have any sort of idea why?

like image 598
Dimitrios Desyllas Avatar asked Jun 30 '26 21:06

Dimitrios Desyllas


1 Answers

Please try calling your extension like this

{{ url | censor }}

like image 90
Alviaz Avatar answered Jul 03 '26 13:07

Alviaz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!