1

I want the content to be customized by the user, so I created a simple theme and added some fields like background image, color, size etc.

{% 
    set background_image = content.field_image.0['#markup'] 
%}
{% 
    set size  = content.field_size.0['#markup'] 
%}
{% 
    set background_color  = content.field_background_color.0['#markup'] 
%}

Is this the right way to do it? Is it a safe way to get content with #markup?

{%
  set section = {
    'section_attributes': create_attribute().addClass([
      'section',
      size|clean_class,
      background_color|clean_class,
    ])
    .setAttribute('style', 'background-image: url(' ~ background_image ~ ');' )
  }
%}

Is there any other safe way to do this?

0

1 Answer 1

2

I think that it would be better to use

{{ content.field_size.value }}

Then twig will automatically get the value from the field.

If you just want to render the field then {{ content.field_size}} should be fine.

Hope it helps.

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.