defining helper proc within a haml file in rails

289 views
Skip to first unread message

Andrew Brown

unread,
Jan 24, 2015, 6:26:54 PM1/24/15
to ha...@googlegroups.com
Sometimes I find it useful to define a little helper proc within a haml view in a rails app, but the syntax I've come up with is pretty horrible.  I'm wondering if there is a good way to do this.  Here's an example:

- strong_name = lambda do |object| capture do
 
- 2.times do
   
%strong= object.name
- end end

= strong_name.call(object)

So I have a few questions
1) Is there a terser/better way to say this?
2) Why can't I use curly braces in place of either do/end?
3) Assume that there is no good answer to #1, would there be value in a helper method that combines this into some sort of 'capture_proc' function that does the capture and the lamda together so you could write:

- strong_name = capture_proc do |object|
 
- 2.times do
   
%strong= object.name



I hope this is the right forum for this sort of question; if it's not, I'd welcome any suggestions about where to post this.  Thanks for your help,

Andrew

Andrew Brown

unread,
Jan 24, 2015, 7:10:58 PM1/24/15
to ha...@googlegroups.com
To folow up, here's the helper I created to do just this:

  def capture_proc
    proc
{ |*args| capture_haml { yield *args } }
 
end


Reply all
Reply to author
Forward
0 new messages