Skip to content

Capturing and rendering a block as string in an ERB template. Is this possible? #77

@mhayes

Description

@mhayes

Just curious, but is it possible to create a helper that captures the content of a block. Specifically I'd love to be able to do something like this in Stasis (this is a simplified example):

In my ERB template:

<% show_for_version('4.0.0') do %>
<p>I would love to display HTML content here.</p>
<% end %>

I would like the HTML output to then look something like this:

<div class='version-4.0.4'>
  <p>I would love to display HTML content here.</p>
</div>

I tried to implement this as a helper method in controller.rb but it does not seem to be doing anything. Here's the helper code I have:

helper do
  def show_for_version(version, &block)
    if block_given?
      "<div class='version-#{version}'>#{yield block}</div>"
    end
  end
end

Is something like this possible? Any help would be mucho appreciated, thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions