Search code examples
erbmiddlemantiltmiddleman-4

Custom helper with block


I'm using the current release 4.3.3 of Middleman.

I'd like to define a custom helper which accepts a block. For the sake of simplicity, here's a nonsensical example of wrap_me which wraps the block content with the given tag.

It should be possible to implement this with capture_html provided by Padrino (which is explicitly mentioned in the Middleman docs):

module CustomHelpers
  def wrap_me(tag, &block)
    captured = capture_html(&block)
    concat_content "<#{tag}>" + captured + "</#{tag}>"
  end
end

Put to use in ERB:

<%= wrap_me('span') do %>
  Hello
<% end %>

Now this raises a SyntaxError on line 274 of tilt-2.0.9/lib/tilt/template.rb which tries to eval a string. It appears, the capture is going beyond the "end".

What am I doing wrong here? How to use capture_html and concat_content if Tilt is preventing helpers from having blocks?

Thanks for your help!

(I'll ask the same question in the Middleman forum.)


Solution

  • Apparently, when using blocks, the equal sign has to be dropped. The following works:

    <% wrap_me('span') do %>
      Hello
    <% end %>