Skip to content
Daniel Berger edited this page Sep 25, 2024 · 4 revisions

Overview

The html-table gem is a library for generating HTML tables using Ruby.

Synopsis

require 'html/table'
include HTML

# Explicit syntax
table = Table.new{ |t|
  t.border  = 1
  t.bgcolor = "red"
}

# Implicit syntax
table = Table.new do
  border   1
  bgcolor 'red'
end

table.push Table::Row.new{ |r|
  r.align   = "left"
  r.bgcolor = "green"
  r.content = ["foo","bar","baz"]
}

row = Table::Row.new{ |r|
  r.align   = "right"
  r.bgcolor = "blue"
  r.content = "hello world"
}

table[1] = row

puts table.html
# Output
<table border=1 bgcolor='red'>
   <tr align='left' bgcolor='green'>  # row 0
      <td>foo</td>                    # column 0
      <td>bar</td>                    # column 1
      <td>baz</td>                    # column 2
   </tr>
   <tr align='right' bgcolor='blue'>  # row 1
      <td>hello world</td>            # column 0
   </tr>
</table>

Clone this wiki locally