I have been playing around a little with Markaby while working on a few projects. I have had some trouble getting rid of tags and replacing with Ruby code. Not because I don’t like, but because it just feels so different. While doing templates this way I started thinking that this really feels similar to a smell I can’t stand.
In attempting to not have any logic in view templates while doing Rails work all logic gets moved to models or helpers. The problem is that generally that means some html tags get moved into helpers as well. Traditionally I have seen this done a few ways. The most common ways I have seen is some form of string concatenation. I find this smells as it just doesn’t feel very Rubyish.
.concat Concatenation
+= Concatenation
<< Concatenation
I do see some use of content_tag, but until recently you could not easily nest using a block. Now in edge it supports passing a block which seems much more elegant.
Content Tag with Blocks
However, seeing Markaby in action being able to set ids and classes right off the element is pretty sexy. Additionally not having to type content_tag a million times is nice. It certainly feels like the most Ruby way to handle the problem.
Markaby with Blocks
If you are a Rubyist tell me how you handle rendering HTML in your helpers. Why do you do it that way?
There is a nice screencast on the topic over at RailsCasts.



















Great post, Derek! I see quite a bit of the += and << concatenation approaches in helpers. After RailsConf 2007, I started using content_tag almost exclusively. I wasn’t aware that you could pass a block to content_tag, but that makes it even more useful.
I haven’t made the leap to Markaby yet. Is it really that much better/clearer? It adds another dependency to your project and gives you another DSL to learn. Now that content_tag supports blocks they seem pretty similar…
I think that its slightly more clear in that you remove the noise of “content_tag” being repeated over and over. This puts the emphasis back on the tag name (imho). Additionally, being able to set an id by doing .idname! or a class by doing .classname has a sexy feel to it.
Vendoring dependencies eliminates a lot of issues normally incurred by adding new dependencies. This makes it less heartburn. Looking at trends in Rails (removing database adapters from core for example) and projects like Merb (removing ORM ) I think that adding dependencies in inevitable because its turning into a assemble your own framework world out of components that already exist.
The more I use Markaby the less I like erb and content_tag. I am almost to the point where .html.mab is more appealing than .html.erb. At that point the context switching between DSL’s is gone. Markaby is so ruby like, it doesn’t much feel like learning a new DSL.
Certainly it is debatable and why I am looking for feedback.