0

I want to add more classes to the title of a node using the following code.

<h3{{ title_attributes.addClass(article-full__title) }}>{{ node.label }}</h3>

It doesn't work, despite the fact title_attributes is described as follows.

title_attributes: Same as attributes, except applied to the main title tag that appears in the template.

I know that is possible to add classes straightly with code similar to the following one.

<h3 Class="article-full__title" }}>{{ node.label }}</h3>

I'm eager to know the standard way of doing this, and I don't want to miss anything.

My class names follow the BEM Name Convention rules and it's hard for me to break and behave contrary to those rules and change my class names.

2

You have to enclose the class in single quotes:

{{ title_attributes.addClass('article-full__title') }}

Otherwise this is interpreted as a variable name. In Twig unknown variables don't trigger an error, they return silently NULL.

  • Thank you so much for your reply, But is there any other way to solve this problem because my class names follow from BEM Name Convention and it's hard for me to breaking those rules and change my class names. – Mojtaba Reyhani Jul 2 '17 at 10:15
  • No, that's not the issue here, of course you can use BEM names. You only have to add the single quotes. – 4k4 Jul 2 '17 at 11:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.