Haml, Sinatra and HTML attributes

I’m in the midst of creating a new web project which is small enough that it has a good chance of getting done. I using it as an excuse/reason to dabble with something new, technology-wise. To balance all the Microsoft that I get at work, I’m playing with Ruby again. Only this time not with Rails. Rails is so much more than I need for this. This time I am playing with HAML, Sinatra and at some point Passenger. If these things don’t interest you, then you have my permission to stop reading now, because it isn’t going to get better.

So far, my experience has been quite pleasant. There seem to be a fair number of resources out there for all of these technologies, and if you stick a couple of the techs together in a search, you can find some articles on integration. A quick intro in case you aren’t keen to visit the pages. Sinatra is a special syntax for Ruby which makes it very easy to create web applications. It can essentially act as a front controller for your application, mapping URLs to blocks of code. HAML on the other hand can be considered a template library—putting it simply. It can provide the views that render into the HTML pages. Passenger is a tool for getting all of this to play nicely with Apache (the web server). I haven’t gotten to this part yet, but fingers crossed, it too will be smooth.

However, the first thing that I noticed after taking a quick peek at the output of my HAML is that all of the attribute values were surrounded by apostrophes. Whoa. It was like getting kicked in the junk by 1998. So something like:

<link rel='stylesheet' href='style.css' type='text/css' />

instead of

<link rel="stylesheet" href="style.css" type="text/css" />

It may not seem like a big deal to you, but it was to me. I went back to the HAML Reference and noticed there was an option called :attr_wrapper which defined which character to use. Knowing that, I tried to figure out how to set that option in Sinatra where I’m using HAML. After some searching I figured out that you can pass options to HAML each time I call it. However this seemed a bit cumbersome. Fortunately there is an answer, which was in the Sinatra Readme. You can set the option globally with the following at the beginning of the Ruby file.

set :haml, {:attr_wrapper => '"'}

I hope that this tidbit makes it out into the world and helps someone else solve this essential issue more quickly.

October 28, 2009
Comments

Comments are not allowed on this item.

Weather

Bermuda: 15°C
Halifax: 0.3°C

Backpack: Get Organized and Collaborate
If the code and comments disagree, both are probably wrong. — Norm Schryer