如何在 Symfony2 中添加新的表单布局?

发布于 2024-12-18 04:08:07 字数 1198 浏览 2 评论 0原文

在我的 Symfony2 项目中,我注意到只有两个用于渲染表单的布局选项:

form_table_layout.html.twigform_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 看起来在正确的位置,但它似乎对表单的渲染没有任何影响(我将表格布局文件的内容复制到新的列表布局文件中以测试理论,并且我的表单仍在使用

< 进行渲染/code> 标签可能会这样。是由我正在使用的 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
and
form_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

锦上情书 2024-12-25 04:08:07

您不必将其放入 app 子文件夹中;您可以将其放入您想要的任何捆绑包中。例如,就我而言,我已将其放入 CommonBundle/Resources/views/Form/fields.html.twig 中,并在 config.yml 文件中将其激活为

twig:
    form:
        resources: [ 'CommonBundle:Form:fields.html.twig' ]

当然,如果将其放入 app 子文件夹中,则只需省略包名称部分即可:

twig:
    form:
        resources: [ ':Form:fields.html.twig' ]

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 into CommonBundle/Resources/views/Form/fields.html.twig and activate it in the config.yml file as

twig:
    form:
        resources: [ 'CommonBundle:Form:fields.html.twig' ]

Of course, if you put it into an app subfolder, you just omit the bundle name part:

twig:
    form:
        resources: [ ':Form:fields.html.twig' ]

要正确覆盖表单布局或添加新表单布局,请将文件放置在 app/Resources/views/Form 目录中,并将 config.yml 调整为如下所示...

twig:
    debug:            %kernel.debug%
    strict_variables: %kernel.debug%
    form:
        resources:
            - ":Form:form_list_layout.html.twig"

To correctly override a form layout or add a new one, place the file in your app/Resources/views/Form directory, and tweak your config.yml to something like this...

twig:
    debug:            %kernel.debug%
    strict_variables: %kernel.debug%
    form:
        resources:
            - ":Form:form_list_layout.html.twig"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文