mote icon indicating copy to clipboard operation
mote copied to clipboard

A Question About Capturing Content

Open evanleck opened this issue 9 years ago • 0 comments

Hello!

I'm trying to create a helper similar to sinatra-contrib's content_for method and have a quick question: how would you stash the content from a block evaluating inside of a mote template into another buffer? In other words, capture the content in the block and display it somewhere else but not where the block resides.

I have the following so far:

def content_for(key, &content)
  @_content_for ||= {}

  if content
    @_content_for[key] ||= []
    @_content_for[key].push Mote.parse(yield(content)).call
  elsif @_content_for[key]
    @_content_for[key].join
  end
end

Which gets called like this:

% content_for(:jumbotron) do
  <h1>Home</h1>
% end

And output like this:

% if content_for(:jumbotron)
  <div class='jumbotron'>
    <div class='container'>
      {{ content_for(:jumbotron) }}
    </div>
  </div>
% end

I'd like to capture that <h1> block and display it elsewhere, removing it from that location. Currently, it shows in both places.

Thanks for your help!

evanleck avatar Jun 08 '16 17:06 evanleck