当我{{render}}一个控制器时,无法使用Twig_Extention

时间:2017-10-27 08:44:40

标签: php symfony twig symfony-3.3

在我的模板中,我渲染以下控制器:

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

按惯例规定的WidgetsController具有以下内容:

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',
          ],
      ]);
  }
}

myWidget.html.twig有以下内容:

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

censor是通过以下twig插件定义的:

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);
    }
}

但是我收到以下Twig_Error_Syntax错误:

Unknown "censor" function.

你们的伙伴们知道为什么吗?在我的services.php上,我有以下设置:

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/*');

所以你们有什么想法吗?

1 个答案:

答案 0 :(得分:2)

请尝试像这样拨打您的分机号

  

{{url |审查员}}