PHP

Tutorials and guides on PHP

CSS classes in Symfony2 twig labels

Written by: Peter Fisher on May 18, 2016
Tagged under: ,
Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

When rendering a Symfony2 form in a twig template I often get caught out when applying css classes to a label.

Label attributes are handled differently from a forms attribute and this often leads to confusion.

Form attributes

Adding css class attributes to a Symfony form field is done like so:

Label attributes

As you can see the parameter attr is supplied with a key of class. This is not the case when adding css class attributes to labels.

Not only is the attr parameter named differently but its also the third parameter of the label control:

So the parameter you need is label_attr. The second parameter is the label value. Set this to null if you want to use the label value defined in the form class.

You can find out more about using labels in twig templates by reading the Symfony2 documentation

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

Peter Fisher is a web developer working in Gloucester UK. Founder of the digital agency Websomatic, author of this blog and the HowToCodeWell Youtube channel. Peter has over ten years of web development experience under his belt

Read all about Peter Fisher

Leave a Reply

Your email address will not be published. Required fields are marked *