当我 {{ render }} 控制器时无法使用 Twig_Extention
Cannot use Twig_Extention when I {{ render }} a Controller
在我的模板中,我渲染了以下控制器:
{{ 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/*');
小伙伴们知道为什么吗?
请尝试这样调用您的分机
{{ url | censor }}
在我的模板中,我渲染了以下控制器:
{{ 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/*');
小伙伴们知道为什么吗?
请尝试这样调用您的分机
{{ url | censor }}