如何在 Symfony2 中添加新的表单布局?
在我的 Symfony2 项目中,我注意到只有两个用于渲染表单的布局选项:
form_table_layout.html.twig
和 form_div_layout.html.twig
这些都位于 symfony\src\Symfony\Bridge\Twig\Resources\views\Form
中,我想在某处添加我自己的一个在我的 app\Resources
目录中。
我遇到的唯一问题是,当我将新的布局文件 (form_list_layout.html.twig
) 添加到 app\Resources\views\Form
时,Symfony 不会查找它并只在默认位置中查找(在 Twig 目录中)。
我已经调整了我的 config.yml 来告诉它包含新的布局,但也许我做错了什么:
twig:
debug: %kernel.debug%
strict_variables: %kernel.debug%
form:
resources: ['form_list_layout.html.twig']
编辑:好的,我现在已经让 Symfony2 看起来在正确的位置,但它似乎对表单的渲染没有任何影响(我将表格布局文件的内容复制到新的列表布局文件中以测试理论,并且我的表单仍在使用 CraueFormFlowBundle
引起的,所以我会检查一下)。
工作config.yml
:
twig:
debug: %kernel.debug%
strict_variables: %kernel.debug%
form:
resources:
- ":Form:form_list_layout.html.twig"
编辑2:看起来与CraueFormFlowBundle
没有任何关系,我的配置中是否有任何内容可能导致它忽略全局表单布局?
编辑:实际上,它正在发挥作用。当我更改布局文件以包含列表元素而不是表格布局内容时,它看起来没问题。
In my Symfony2 project, I noticed that there are only two layout options for rendering forms:
form_table_layout.html.twig
andform_div_layout.html.twig
These both reside in symfony\src\Symfony\Bridge\Twig\Resources\views\Form
, and I would like to add one of my own somewhere in my app\Resources
directory.
The only issue I'm having, is when I add my new layout file (form_list_layout.html.twig
) into app\Resources\views\Form
, Symfony doesn't look for it and just looks in the default location instead (in the Twig directory).
I have tweaked my config.yml
to tell it to include the new layout, but maybe I'm doing something wrong:
twig:
debug: %kernel.debug%
strict_variables: %kernel.debug%
form:
resources: ['form_list_layout.html.twig']
EDIT: Okay, I've got Symfony2 looking in the right place now, but it doesn't seem to have any effect on the rendering of the form (I copied the contents of the table layout file into my new list layout file to test the theory, and my form is still rendering with <div>
tags instead. This might be caused by the CraueFormFlowBundle
I'm using, so I'll check it out).
The working config.yml
:
twig:
debug: %kernel.debug%
strict_variables: %kernel.debug%
form:
resources:
- ":Form:form_list_layout.html.twig"
EDIT 2: Doesn't look like its anything to do with the CraueFormFlowBundle
, is there anything in my configuration that might be causing it to ignore the global form layout?
EDIT: Actually, it is working. As soon as I changed my layout file to include my list elements instead of the table layout content, it appeared okay.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不必将其放入
app
子文件夹中;您可以将其放入您想要的任何捆绑包中。例如,就我而言,我已将其放入CommonBundle/Resources/views/Form/fields.html.twig
中,并在config.yml
文件中将其激活为当然,如果将其放入
app
子文件夹中,则只需省略包名称部分即可:You don't have to place it into an
app
subfolder; you can place it in any bundle you want. For example, in my case, I've placed it intoCommonBundle/Resources/views/Form/fields.html.twig
and activate it in theconfig.yml
file asOf course, if you put it into an
app
subfolder, you just omit the bundle name part:要正确覆盖表单布局或添加新表单布局,请将文件放置在
app/Resources/views/Form
目录中,并将config.yml
调整为如下所示...To correctly override a form layout or add a new one, place the file in your
app/Resources/views/Form
directory, and tweak yourconfig.yml
to something like this...