如何从数据库中渲染Twig内容(覆盖渲染函数)?

huangapple go评论100阅读模式
英文:

How to render twig content from database (overwrite render function)?

问题

由于几个原因,我将一些Twig模板存储在我的数据库中(包括Twig路径和HTML标记)。

我想要实现以下目标:

当我在我的控制器中调用render函数(继承自Controller)时,我希望首先检查数据库是否有模板,如果没有,则像往常一样返回Twig模板。

然而,由于我有50个控制器,最好的方法是重写render函数,这样我的调用保持不变:

return $this->render('@testfrontend/MyAccount/index.html.twig');
英文:

For several reasons i stored some twig templates in my database (twig path + html markup).

I want to achieve the following:

When i call the render function in my controller (extends Controller), i want the database to be checked first to see if there is a template, otherwise it returns the twig template as usual.

However, because I have 50 controllers, overriding the render function would be best, so my call stays the same:

return $this->render('@testfrontend/MyAccount/index.html.twig');

答案1

得分: 0

根据@VincentDecaux的建议:

> 你应该创建一个继承Symfony AbstractController的新类,然后重写renderView方法,并使你的控制器继承你自己的类。在现代IDE中可以很容易地完成这个操作。

更具体地说,

  • 官方文档展示了如何自定义表单渲染:https://symfony.com/doc/current/form/form_customization.html

  • 覆盖自定义主题模板:https://drupal.stackexchange.com/questions/227268/how-do-i-override-theme-custom-twig-in-my-module-to-render-input-fields

英文:

As suggested by @VincentDecaux

> You should create a new class extending the Symfony
> AbstractController, then override the renderView method, and make your
> controllers extend your own class. It can be done easily with modern
> IDE.

On a more granular level,

huangapple
  • 本文由 发表于 2023年8月8日 21:03:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76859850.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定