Wrapper Helper Help Please

Hi all. I’m hoping this is a simple one that someone can put me right
on.

I have a view that ‘wraps’ some div’s around a Page Header and Page
Content, for formatting reasons:

'Header Here'
'Content Here'

I use this ‘wrapper’ a lot, so I thought I’d put it into a helper,
thus:

def lhs_block(title)
content_tag(:div,
content_tag(:div,
content_tag(:div,title,:class=>‘PageHeader’) +
content_tag(:div,yield,:class=>‘PageContent’),
:class=>‘Page’),
:id=>‘LHS’)
end

This works fine in the view when I do this:

<%= lhs_block(‘Header Here’) {‘Content Here’} %>
Unfortunately, the content is usually more complex that this and multi-
line (as you can imagine), so when I tried to do it this way:

<%= lhs_block(‘Header Here’) do %>
Content Here
<% end %>

All hell broke loose:

compile error
./script/…/config/…/app/views/contacts/index.rhtml:1: parse error,
unexpected ‘)’
_erbout = ‘’; _erbout.concat(( lhs_block(list_filter(‘list_wrapper’))
do ).to_s); _erbout.concat “\n”

^
./script/…/config/…/app/views/contacts/index.rhtml:10: parse error,
unexpected kEND, expecting ‘)’

I thought this was how it was done, but maybe I’m wrong.

If anyone can see anything glaringly obvious that I’ve done wrong,
then any help would be appreciated. Or maybe another way to DRY this
code up so that it’s easier to re-use.

Cheers,

Steve A.

On 10 Nov 2007, at 09:58, Steve A wrote:

<% end %>

All hell broke loose:

Judging by similar stuff in rails itself,

def lhs_block(title, &block)
content = capture(&block)
concat(
content_tag(:div,
content_tag(:div,
content_tag(:div,title,:class=>‘PageHeader’) +
content_tag(:div,content,:class=>‘PageContent’),
:class=>‘Page’),
:id=>‘LHS’), block.binding)
end

seems to do the trick.

Fred

I would like to leave a note: It seems like you are misinterpreting
part of the views, models and helpers concept on rails.
Please, take this with some salt because I’m kinda new to rails…

You seem to be replacing a lot of div tags that apparently are
creating the content header and the content of your website.

I would advise keeping those in a layout, maybe your ‘general’ layout
if it is used in most of your pages. This would be very helpful in
case you decide to do some changes in the layout, or a new template.

And if this code is not on most of your pages, then maybe you can use
a partial to help.

I usually use helper methods to create html tags that are not related
to the template, like, link_to_if_authorized (this is a helper I’ve
written today), and those div’s seem to be a big part of your
template.

The point is, avoid creating large blocks of html code outside
an .rhtml file. This will make the job of putting a layout together a
lot easier.

IMO, layouts and views are 90% HTML, Helpers are 10% tops (I currently
have 2 of them on ApplicationHelpers and none has HTML code),
controllers and models are 0% HTML.

Take this as a light advice, I could have gotten a totally wrong
picture.

Best regards,

Felipe

Thanks Fred, that works a treat (as long as you use <% instead of <
%= ). I was obviously being too simplistic, and will need to take a
deeper look at why this works when mine didn’t.

Cheers,

Steve A.

On Nov 10, 7:19 pm, Frederick C. [email protected]