英文:
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,
-
the official documentation shows how to Customize Form Rendering,
https://symfony.com/doc/current/form/form_customization.html -
Overriding a custom theme template https://drupal.stackexchange.com/questions/227268/how-do-i-override-theme-custom-twig-in-my-module-to-render-input-fields
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论