Module: Html

Defined in:
lib/html/base.rb

Overview

Html provides a concise way of programmatically expressing HTML tags and structure.

This enables one to express a list like this:

Html::UnorderedList.new << (Html::ListItem.new << "first") + (Html::ListItem.new << "second")

which results in:

<ul><li>first</li><li>second</li></ul>

The << operator means “contains”, and reduces an Html object and a string to another string. So after the << operation, the tags just obey String semantics.

Commonly used nested tags can be composited and deployed as templates multiple times.

title_link = Html::H1.new << Html::Link(:href => "http://mysite.com")
title_link << "My Site"

which results in:

<h1><a href="http://mysite.com">My Site</a></h1>

This is seemingly more verbose that writing it directly, but it enables you to write HTML without having to do string interpolation and concatenation, and it doesn’t imbue Ruby code with a lot of HTML-related strings.

words = %w(hello doctor name continue yesterday tomorrow)
Html::UnorderedList.new << words.collect {|w| Html::ListItem.new(:class => "class-#{w}") << w}.join

which will yield:

<ul>
  <li class="class-hello">hello</li>
  <li class="class-doctor">doctor</li>
  <li class="class-name">name</li>
  <li class="class-continue">continue</li>
  <li class="class-yesterday">yesterday</li>
  <li class="class-tomorrow">tomorrow</li>
</ul>

Defined Under Namespace

Classes: Base, Break, Comment, Composite, Container, Div, Flag, H1, H2, H3, H4, Image, Item, Link, ListItem, OrderedList, Paragraph, Span, UnorderedList