Skip to content

How to render component with slots that accept HTML? #624

Answered by allmarkedup
pinzonjulian asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @pinzonjulian,

There are a number of ways to add markup to the component block content when rendering previews.

One thing to keep in mind whichever method you use is that when in a Ruby context and not in a template (ERB) context only the last line of the block will be used as the block content (just like the implict return value in Ruby methods).

The simplest way is probably to add markup as a string and tag it as .html_safe` like this:

class DisclosureComponentPreview 
  def default
    render DisclosureComponent.new(title: "Open disclosure") do
      "<h2>This goes inside the disclosure</h2><p>It can be a multi-line block of HTML with multiple tags</p>".html_safe
    end
  end
end

(…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@pinzonjulian
Comment options

Answer selected by pinzonjulian
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants