using heredoc in merb view

0 views
Skip to first unread message

jakemiles

unread,
Dec 4, 2009, 5:17:21 AM12/4/09
to merb
Hi all. I'm trying to write a helper function that will let me use
something like the following in my view, where I can pass in a text
block as the last argument:

<%= code "java", <<-CODE

// create a panel using a GridBagLayout
JPanel panel = new JPanel (new GridBagLayout());

// three labels on the first row
panel.add (new JLabel ("One"));
panel.add (new JLabel ("Two"));
panel.add (new JLabel ("Threeeee"));

CODE %>

but this produces this error in the merb console (and merb dies):

can't find string "CODE" anywhere before EOF

So it can't find the end of the heredoc. Does anyone know how to get
this to work? I suspect it's more of an erb issue than merb, but I
thought the hacker mentality might be able to solve this one. :)

Thanks for any help.

- Jake

jakemiles

unread,
Dec 4, 2009, 2:53:21 PM12/4/09
to merb
The answer, I learned from a reply on the Ruby thread, requires two
smalll changes, shown in the code here:

<%= code "java", <<-CODE.chomp

// create a panel using a GridBagLayout
JPanel panel = new JPanel (new GridBagLayout());

// three labels on the first row
panel.add (new JLabel ("One"));
panel.add (new JLabel ("Two"));
panel.add (new JLabel ("Threeeee"));

CODE
%>

Call chomp on heredoc delimiter (which, I infer, is a ruby string
object at the time of parsing), and make sure the %> is on its own
line after the end delimiter.

Very cool - this makes a lot of cool helper methods possible. For
example, if you want to parse a block of HTML to modify it you can do
that fairly unobtrusively this way.
Reply all
Reply to author
Forward
0 new messages