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: <div id="LHS"> <div class="Page"> <div class="PageHeader"> ''Header Here'' </div> <div class="PageContent"> ''Content Here'' </div> </div> </div> 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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 10 Nov 2007, at 09:58, Steve A wrote:> > 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: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
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 Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 10 Nov 2007, at 09:58, Steve A wrote: > > > > > 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: > > 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 > > smime.p7s > 5KDownload--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
felipekk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Nov-11 10:52 UTC
Re: Wrapper Helper Help Please
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 On Nov 10, 5:09 am, Steve A <steve...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > On 10 Nov 2007, at 09:58, Steve A wrote: > > > > 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: > > > 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 > > > smime.p7s > > 5KDownload--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---