如果您伸出Twig,那很容易。
首先,创建一个包含扩展名的类:
<?phpnamespace AcmeDemoBundleTwigExtension;use SymfonyComponentDependencyInjectionContainerInterface; use Twig_Extension;class VarsExtension extends Twig_Extension{ protected $container; public function __construct(ContainerInterface $container) { $this->container = $container; } public function getName() { return 'some.extension'; } public function getFilters() { return array( 'json_depre' => new Twig_Filter_Method($this, 'jsonDepre'), ); } public function jsonDepre($str) { return json_depre($str); }}然后,在您的Services.xml文件中注册该类:
<service id="some_id" > <tag name="twig.extension" /> <argument type="service" id="service_container" /></service>
然后,在您的Twig模板上使用它:
{% set obj = form_label(category) | json_depre %}


