Symfony 4:添加 Twig_Extension_StringLoader [英] Symfony 4: Add Twig_Extension_StringLoader

查看:35
本文介绍了Symfony 4:添加 Twig_Extension_StringLoader的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Symfony 4 项目中,我有一个包含 HTML 的变量,我尝试用 Twig 显示它.我想使用 template_from_string() 函数,但我必须将 Twig_Extension_StringLoader 添加到 Twig 环境中.

In my Symfony 4 project, I have a variable containing HTML that I try to display with Twig. I want to use the template_from_string() function but I have to add Twig_Extension_StringLoader to the Twig environment.

我没有找到任何信息可以在 Symfony 4 中进行这种简单的操作.

I didn't find any information to do this simple manipulation in Symfony 4.

推荐答案

在这些谜团之一中,默认情况下不加载字符串加载器扩展.一直不明白为什么.看起来足够无害.

In one of those mysteries, the string loader extension is not loaded by default. Never understood why. Seems harmless enough.

S4 只需要在您的服务文件中添加一行:

S4 just needs a single line in your services file:

# config/services.yaml
services:
    Twig\Extension\StringLoaderExtension:

然后您可以使用bin/console debug:twig | grep string"来确认它可用.

You can then use "bin/console debug:twig | grep string" to confirm it is available.

这篇关于Symfony 4:添加 Twig_Extension_StringLoader的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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