Hey guys, There is something I have been trying to implement but so far no luck. Basically, I want to have helpers that generate HTML around given content, similar to ActionView''s form_for. Besides, I want to keep that "framing" HTML in separate files, similar to templates. Ideally I''d even love to suggest it as a feature for Rails. Let me show you an example. This is what I want to have in my rhtml file: <% bubble_block :corners => "round", :color => "brown" do %> <div id="content">My content</div> <% end %> So far I have tried the following: I added a method to my application helper: def bubble_block(options, &proc) raise ArgumentError, "No block given" unless block_given? rendered_html = render(:partial => "/templates/bubble", :locals => {:block => proc} ) concat rendered_html, proc.binding end This is how _bubble.rhtml looks like: <div id="bubble"> <% block.call %> </div> What happens is *ALMOST* what I want, except my block gets rendered twice. The output looks like this: <div id="content">My content</div> <div id="bubble"> <div id="content">My content</div> </div> What am I missing? Thank you! --~--~---------~--~----~------------~-------~--~----~ 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 May 2, 2007, at 2:26 PM, zeka wrote:> Hey guys, > > There is something I have been trying to implement but so far no luck. > Basically, I want to have helpers that generate HTML around given > content, similar to ActionView''s form_for. Besides, I want to keep > that "framing" HTML in separate files, similar to templates. > > Ideally I''d even love to suggest it as a feature for Rails. Let me > show you an example. > This is what I want to have in my rhtml file: > > <% bubble_block :corners => "round", :color => "brown" do %> > <div id="content">My content</div> > <% end %> > > So far I have tried the following: I added a method to my application > helper: > > def bubble_block(options, &proc) > raise ArgumentError, "No block given" unless block_given? > rendered_html = render(:partial => "/templates/bubble", :locals => > {:block => proc} ) > concat rendered_html, proc.binding > end > > This is how _bubble.rhtml looks like: > > <div id="bubble"> > <% block.call %> > </div> > > What happens is *ALMOST* what I want, except my block gets rendered > twice. The output looks like this: > > <div id="content">My content</div> > <div id="bubble"> > <div id="content">My content</div> > </div> > > What am I missing? Thank you!Since you don''t say what options is supposed to do, perhaps this give you an idea of how to proceed: def bubble_block options={}, &block raise ArgumentError, "No block given" unless block_given? content_tag :div, {:id => ''bubble''}.merge(options), &block end and forget about the unnecessary _bubble.rhtml -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thank you for replying, Rob. I already know how to do it without external .rhtml (I can use concat() inside of bubble_for). However, the entire idea is about using external .rhtml files for surrounding HTML, since there is a lot of HTML and having that HTML embedded into ruby code is not practical (designers need to touch it). What exactly in my implementation causes the wrapped block to render twice? Inside and outside of the surrounding HTML? On May 2, 1:51 pm, Rob Biedenharn <R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org> wrote:> On May 2, 2007, at 2:26 PM, zeka wrote: > > > > > Hey guys, > > > There is something I have been trying to implement but so far no luck. > > Basically, I want to have helpers that generate HTML around given > > content, similar to ActionView''s form_for. Besides, I want to keep > > that "framing" HTML in separate files, similar to templates. > > > Ideally I''d even love to suggest it as a feature for Rails. Let me > > show you an example. > > This is what I want to have in my rhtml file: > > > <% bubble_block :corners => "round", :color => "brown" do %> > > <div id="content">My content</div> > > <% end %> > > > So far I have tried the following: I added a method to my application > > helper: > > > def bubble_block(options, &proc) > > raise ArgumentError, "No block given" unless block_given? > > rendered_html = render(:partial => "/templates/bubble", :locals => > > {:block => proc} ) > > concat rendered_html, proc.binding > > end > > > This is how _bubble.rhtml looks like: > > > <div id="bubble"> > > <% block.call %> > > </div> > > > What happens is *ALMOST* what I want, except my block gets rendered > > twice. The output looks like this: > > > <div id="content">My content</div> > > <div id="bubble"> > > <div id="content">My content</div> > > </div> > > > What am I missing? Thank you! > > Since you don''t say what options is supposed to do, perhaps this give > you an idea of how to proceed: > > def bubble_block options={}, &block > raise ArgumentError, "No block given" unless block_given? > content_tag :div, {:id => ''bubble''}.merge(options), &block > end > > and forget about the unnecessary _bubble.rhtml > > -Rob > > Rob Biedenharn http://agileconsultingllc.com > R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---