Get field values in Twig with #markup 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?
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.