Twig 模板内的斜线 [英] stripslashes inside Twig template

查看:20
本文介绍了Twig 模板内的斜线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 twig 模板中使用 php stripslashes 函数,但该函数不是标准的 twig 函数,所以我必须将它添加到 twig 作为扩展名,我在控制器中尝试了此代码,但它不起作用:

i want to use the php stripslashes function inside a twig template but this function is not a standard twig function, so i have to add it to twig as an extension, i tried this code inside a controller, but it doesnt work:

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
class XController extends Controller
{

public function YAction($page)
{
$twig=$this->get('twig');
$twig->addFunction('functionName', new Twig_Function_Function('someFunction'));
...

我是否需要Twig_Function_Function"类的 use 语句?我做错了吗?

do i need a use statement for the "Twig_Function_Function" class? am i doing this wrong?

推荐答案

如果你想在你的树枝模板中使用它,你不需要在你的控制器中进行任何添加或调用,阅读如何编写自定义 Twig 扩展 文档部分.

If you want to use it in your twig templates, you don't need to make any add or call inside your controller, Read the How to write a custom Twig Extension section of the documentation.

基本上,您需要创建一个 扩展扩展 \Twig_Extension 的类,那么你需要 使用 twig.extension 标签将其注册为服务.最后你需要实现 getFunctions()方法以添加自定义的树枝功能.

Basicaly, you need to create an Extension Class that extends \Twig_Extension , then you need to register it as a service using the twig.extension tag. And finally you need to implement the getFunctions() method in order to add customized twig functions.

但在你的情况下更好的是添加一个过滤器,使用相同的逻辑你也可以添加一个 getFilters() 方法,以便您可以指定自定义过滤器.

But in your case better is to add a filter, with the same logic you can also add a getFilters() method in your extension class so that you can specify your customized filters.

此外,请深入查看文档的Extending Twig 部分以了解树枝可以延伸的所有方式.

Also, take a deeper look at the Extending Twig section of the documentation to understand all the ways twig can be extended.

这篇关于Twig 模板内的斜线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆