Hampton must still be using Haml 1.0 :-). Here's an updated helper
method that should work on the latest version of Haml (needs to be
haml_concat instead of puts):

def text_field_with_label(f, name, label_override = nil, options =
{})
  haml_tag :p do
    haml_concat f.label(name, label_override)
    haml_concat f.text_field(name, options)
  end
end


Unfortunately, I wouldn't exactly call this great code. Main issue is
outputting html directly to the response buffer. It's good practice to
always have your helpers return a string, so its more declarative what
you're doing (outputting html, instead of running a method). Luckily
its easy to do by just wrapping it with a capture_haml block

def text_field_with_label(f, name, label_override = nil, options =
{})
  capture_haml do
    haml_tag :p do
      haml_concat f.label(name, label_override)
      haml_concat f.text_field(name, options)
    end
  end
end

=  text_field_with_label(f, :name)
=  text_field_with_label(f, :address)
=  text_field_with_label(f, :country)


Also, I'd advise checking out some Rails form builders. Here's a good
one I use: http://github.com/neorails/form_fu

On Mar 15, 8:07 pm, Hampton Catlin <[email protected]> wrote:
> No problem.
>
> Swear to god, if you find yourself thinking an old habit is hard in  
> haml, it's usually a bad habit.
>
> - Hampton
>
> On Mar 15, 2009, at 6:54 PM, gdelfino <[email protected]> wrote:
>
>
>
> > Hampton, thank your for your help or.. should I say helpers?
>
> > Regards,
>
> > Gustavo
>
> > On Mar 16, 5:49 pm, Hampton <[email protected]> wrote:
> >> So, Haml is *not* good at taking ugly code and making it pretty.  
> >> Its good at
> >> making you make sure you do good stuff.
> >> def text_field_with_label(f, name, label_override = nil, options =  
> >> {})
> >>   haml_tag :p do
> >>     puts f.label(name, label_override)
> >>     puts f.text_field(name, options)
> >>   end
> >> end
>
> >> then, you can do...
>
> >> - text_field_with_label(f, :name)
> >> - text_field_with_label(f, :address)
> >> - text_field_with_label(f, :country)
> >> .... etc....
>
> >> making it even more dry, i'd probably do this.
>
> >> def field_sets(f, *names)
> >>   names.each do |data|
> >>     name, label_override = *data
> >>     puts text_field_with_label(f, name, label_override)
> >>   end
> >> end
>
> >> - field_sets(f, :name, :address, :country, [:uri,  
> >> "URI"], :postal_code)
>
> >> Also, let's say we kept this simple and a little WET. What is so  
> >> wrong with
> >> doubling the lines.
> >> Its less characters than the erb! So what if its on a new line?
>
> >> Fin.
>
> >> -hampton.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Haml" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [email protected]
For more options, visit this group at http://groups.google.com/group/haml?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to