Thamble¶ ↑
Thamble creates HTML tables from enumerable objects.
Installation¶ ↑
gem install thamble
Source Code¶ ↑
Source code is available on GitHub at github.com/jeremyevans/thamble
Examples¶ ↑
The Thamble.table method expects an enumerable of arrays (such as an array of arrays), and returns a string containing the HTML table output:
puts Thamble.table([[1, 2]])
output:
<table> <tbody> <tr> <td>1</td> <td>2</td> </tr> </tbody> </table>
If a block is passed to the method, all items in the enumerable are yielded to the block, and the block should return an array with the data to use in the table:
puts Thamble.table([{:a=>1, :b=>2}, {:a=>3, :b=>4}]){|l| [l[:b], l[:a] + 10]}
output:
<table> <tbody> <tr> <td>2</td> <td>11</td> </tr> <tr> <td>4</td> <td>13</td> </tr> </tbody> </table>
You can use the :headers option to set custom headers:
puts Thamble.table([[1, 2], [3, 4]], :headers=>['A', 'B'])
output:
<table> <thead> <tr> <th>A</th> <th>B</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>2</td> </tr> <tr> <td>3</td> <td>4</td> </tr> </tbody> </table>
Other options supported are:
- :caption
-
A caption for the table
- :column_th
-
Use th instead of td for first cell in each row
- :table
-
HTML attribute hash for the table itself
- :td
-
HTML attribute hash for the table data cells, can be a proc called with the data value, row position, and row that returns a hash
- :th
-
HTML attribute hash for the table header cells, can be a proc called with the header that returns a hash
- :tr
-
HTML attribute hash for the table rows, can be a proc called with the row that returns a hash
- :widths
-
Array of widths for each column
Escaping ¶ ↑
By default, Thamble escapes all output. You can use the Thamble.raw method to return a string marked as already being escaped.
puts Thamble.table([['&1234;', Thamble.raw('&1234;')]])
output:
<table> <tbody> <tr> <td>&1234;</td> <td>&1234;</td> </tr> </tbody> </table>
The raw method is also exposed on the second object yielded to Thamble.table:
puts Thamble.table([['1', '&1234;']]) do |row, table| row.map{|value| table.raw(value )} end
Rails Support¶ ↑
Thamble comes with basic rails support via:
require 'thamble/rails' ApplicationController.helper Thamble::RailsHelper
This allows you to use the following style in your templates:
<%= thamble([[1, 2], [3, 4]], :headers=>['A', 'B']) %>
License¶ ↑
MIT
Author¶ ↑
Jeremy Evans <code@jeremyevans.net>