如何获得由 Symfony 生成的格式化 HTML [英] How to get formatted HTML generated by Symfony

查看:27
本文介绍了如何获得由 Symfony 生成的格式化 HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆