我使用 Symfony2 来生成表单,或者实际上是 Twig,它使用了 Symfony 的函数,所以视图看起来像这样:
I'm using Symfony2 to generate forms, or actually Twig, which uses Symfony's functions, so the view looks like this:
{{ form_start(form) }} {{ form_widget(form) }} {{ form_end(form) }}这会生成没有任何换行符或缩进的 HTML 代码.当我查看 HTML 源代码时,这是不可读的.
This generates HTML code without any newlines or indentation. This is unreadable when I look into HTML source code.
有没有办法强制 Symfony/Twig 对生成的 HTML 进行格式化?
Is there any way to force Symfony/Twig to format the generated HTML?
推荐答案这个 link 漂亮地解释了 twig 中的表单定制.有多种方法可以将 css 样式应用于表单元素.例如,如果您有一个带有 name 字段的表单,您可以通过这种方式向其中添加一个 css 类:
This link explains form customization in twig beautifully. There are numerous ways of applying css styling to form elements. For example, if you have a form with a field name you can add a css class to it this way:
{# render a widget, but add a "foo" class to it #} {{ form_widget(form.name, {'attr': {'class': 'foo'}}) }}或标签:
{{ form_label(form.name, 'Your Name', {'label_attr': {'class': 'foo'}}) }}然后您可以使用 css 类根据您的设计呈现表单元素.希望你明白.答案中提供的 docs 和链接在这方面都很有用.
Then you can use the css classes to render form elements as per your design. Hope you get the idea. The docs and link provided in the answer are both useful in this regard.
更多推荐
如何获得由 Symfony 生成的格式化 HTML
发布评论