gpt4 book ai didi

php - 如何用 Twig 渲染字符串/数据库中的内容?

转载 作者:行者123 更新时间:2023-12-03 18:20:09 24 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





What to use instead of Twig_Loader_String

(7 个回答)


上个月关闭。




由于包括内容翻译在内的几个原因,我不得不构建一个简单的 CMS 来呈现我的 Symfony2 应用程序的页面。
我的问题是似乎不可能从字符串呈现内容。 Twig 只接受文件。我的内容可能包含诸如语言环境或类似的动态部分,因此 Twig 的渲染能力将非常有用。
我尝试使用 TwibstringBundle 渲染它,但它的功能非常有限,并且不能与路径功能一起使用。

最佳答案

http://twig.sensiolabs.org/doc/functions/template_from_string.html
http://symfony.com/doc/current/cookbook/templating/twig_extension.html#register-an-extension-as-a-service

{% include template_from_string("Hello {{ name }}") %}
{% include template_from_string(page.template) %}

由于默认情况下不加载字符串加载器,因此您需要将其添加到您的配置中。
# src/Acme/DemoBundle/Resources/config/services.yml
acme.twig.extension.loader:
class: Twig_Extension_StringLoader
tags:
- { name: 'twig.extension' }

其中 Acme/acme 是您的应用程序名称,DemoBundle 是您要为其启用它的包。

关于php - 如何用 Twig 渲染字符串/数据库中的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9803353/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com