markup.become(:beautiful)

In my brief exploration of Seaside one of the points that caught my eye was that the components render themselves in HTML.

 html table:    [html tableRow:        [html tableData: [html bold: 'Name'].         html tableData: person name].     html tableRow:        [html tableData: [html bold: 'Age'].         html tableData: person age]]

Two points: firstly - I read in one interview (sorry, can’t find the link) that this leaves the designer to control the CSS and the programmer to control the HTML. This immediately struck me as a good thing - for the HTML to be properly CSSable it needs to be well-structured and semantically rich. Which you should do anyway but it’s easy to forget. And secondly, that code snippet above looks great compared to your typical .rhtml file (I think it’s something to do with the angle brackets in HTML which make it hard to read).

So, in the interests of making my application more beautiful, I started investigating how to do something similar in Rails. Which lead me to Markaby - Markup in Ruby. Install the plug-in and, instead of creating rhtml views you create mab views.

Which look like this:

div.contents! do  error_messages_for :booking  form_for :booking, :url => booking_path(@public_course, @booking).to_s, :html => {:method => :put} do | form |    h3 "Customer Details: "    p do       label "Contact: "      collection_select :booking, :contact_id, @contacts, :id, :name    end    p do      label "Notes: "      text_area :booking, :notes, :rows => 5    end    p.explain "Add any arbitrary notes here"    p do      blank_label      submit_tag "Save"      text " or "      link_to "cancel", booking_path(@public_course, @booking), :confirm => 'Are you sure?'    end  endend

Doesn’t that make you feel good? Especially the p.explain (which outputs a P tag with the class “explain”) - immediately you can see how semantic markup becomes integral to your views.

I did have a few problems with the helper methods - I had to set @output_helpers = false and I switched from using the form.text_field :method helpers to the old-style text_field :object, :method helpers (which also meant losing my labelled_form_builder). But it’s a small price to pay for such superior looking markup.

This entry was posted on Thursday, April 19th, 2007 at 7:05 pm and is filed under Beautiful Code, Ruby on Rails and Software Development. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply