I'm desperately trying to render a style attribute, inside a paragraph's template. But all of these variations:

  set backgroundUrl = content.field_slider_background|render|naked|replace({' ': '', "\\n": '', "\\r": ''})

<div class="wrapper">
  <div class="background-item" style="background-image: url({{ backgroundUrl }});">


  set backgroundAttributes = create_attribute()

<div{{ backgroundAttributes.addClass('background-item').setAttribute('style', 'background-image: url(' ~ backgroundUrl ~ ')') }}>

and even:

<div class="background-item" style="z-index: 0;">


<div class="background-item" {{ 'style="...' }}>

just return <div class="background-item">. Other tags are fine.

Does twig generally strip style tags?! Using the latest D8 version...

Edit: outputting backgroundUrl actually gives me the correct string, so writing a tag like data-test="{{ backgroundUrl }}" works nicely


I've done it to print a background color like this in a paragraph template

<div class="container color-{{ content.field_text_color[0] }}" style="background: #{{ content.field_card_color[0] }};">

And it just worked fine. Can you give us the full template maybe we can figure out why it doesn't work.

  • i figured so far that the style tag is being remove when i render the paragraph field as a block. im going to open a new question instead. thanks so far!
    – Alex
    Jul 4 '17 at 15:02
  • if you still want to help me out: drupal.stackexchange.com/questions/239921/…
    – Alex
    Jul 4 '17 at 15:18
  • @Alex, this is the correct answer to the question here, you can use style in templates, but you can't render the paragraph in a block like you do in the follow up question without getting problems with the XSS filter.
    – 4k4
    Jul 4 '17 at 15:22

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.