DRYing up Views 2
Unfortunately, I missed my first tutorial at RailsConf today due to air travel nightmares. However, I was able to make it to a presentation by Bruce Williams and Marcel Molina, Jr. entitled “When V is for Vexing: Patterns to DRY Up Your Views”.
A few points I took away from this:
- It really IS possible to create views that are quite readable: even by designers, not developers.
- Block helpers are where it’s at. You can do much of the same stuff with block helpers that you can do with partials:
- Block helpers read better then a bunch of if statements or other conditional logic.
- You can use parameters (just like you would use local variables) except with helper methods you can properly document them.
- You can wrap stuff around some variable content. Such as:
fancy_box_with_borders do
<p>Stuff inside box.</p>
endThis talk also lit a fire under me to start checkout out templating language alternatives to ERB. I downloaded haml and started converting some of my Find Mini Golf version 2.0 templates over to it. So far, I really like it. It takes some getting used to but seems very readable and much more concise.
I’m not sure if it would go over well with outside designers we often have to work with. If we can get them to be dedicated to learning the Rails Way to do stuff, I think they will definitely appreciate the simplicity of things like haml. But I think it might be difficult to convince some designers to put the extra effort in to learning.
