Markaby is a Ruby library that lets you write HTML in Ruby. It's built into the Camping framework as the templating engine. To create elements, you call a method of that name.

</p>
 
<h1>In Camping, you don't have to set up the Builder.</h1>
 
<h1>I'll leave it out in the other examples.</h1>
 
<p>mab = Markaby::Builder.new
mab.p "This is a paragraph."
mab.div "I can do the same thing with a div."

To nest your HTML, you can pass a block.

form :method => 'post' do
  legend 'Import stuff'
  textarea :name => 'queries', :rows => '25', :cols => '100'
  input :type => 'submit', :value => 'Import!'
end

[A side note, the textarea method inserts some whitespace by default. Very annoying, so if you know how to get rid of it in a non-hacky way, please let me know.]

It uses lots of metaprogramming trickery to make your life a whole lot easier. For example, you can add an arbitrary class to an element by calling the class name as a method.

div.example "This div will be of class example."

The class methods can also be chained.

div.example.special "This div will be of class example <em>and</em> special."

You can do the same thing with an id by simply appending a bang (!) to the class name.

div.main<em>content! "This div will have the id main</em>content."

But what if you want a variable class name? I can't just tack the variable name on because the magic will mean that the class will be the name of the variable, not the value.

%w[one two three].each {|c| puts mab.p.c.to_s}

That's not going to work. It will output this.

<p class="c">
<p class="c">
<p class="c">

Ruby's send method to the rescue.

%w[one two three].each {|c| puts mab.p.send(c)}

This correctly outputs this.

<p class="one">
<p class="two">
<p class="three">