Pat Newell
Pat Newell

Reputation: 2294

Using Blocks in erb's (Sinatra, not Rails)

I would like to use the ruby block/yield concept in an erb (to be specific, so that I can use form_helpers). When I do so I get syntax errors, but not in my erb code; in the outputted code once erb's have been processed.

For example, let's say I have the following helper:

def test_method   # assume this returns some string
    yield if block_given?
end

and I have this erb code:

<%= test_method do %>
    <h1>asdf</h1>
<% end %>

the result is:

syntax error, unexpected ')' ; @_out_buf.concat(( test_method do ).to_s)

I understand that the first line is converted to code independently of the other lines. What I don't understand is why or how to fix it. thanks!


ruby: 1.9.3-p392

sinatra: 1.42

anything else I can provide?

Upvotes: 2

Views: 737

Answers (1)

icanhazbroccoli
icanhazbroccoli

Reputation: 1035

Just remove output helper before test_method call in erb so it looks like:

<% test_method do %>

= helper expects a singleline expression to be given, not multiline.

More detailed info could be found here, here and here.

Upvotes: 3

Related Questions