Module: Thamble

Defined in:
lib/thamble.rb,
lib/thamble/rails.rb

Overview

Thamble exposes a single method named table to make it easy to generate an HTML table from an enumerable.

Defined Under Namespace

Modules: RailsHelper, Raw Classes: RawString, Table, Tag

Constant Summary collapse

OPTS =

Empty frozen hash used for default option hashes

{}.freeze

Class Method Summary collapse

Class Method Details

.raw(s) ⇒ Object

Return a raw string, which will not be HTML escaped in Thamble’s output.



40
41
42
# File 'lib/thamble.rb', line 40

def raw(s)
  RawString.new(s)
end

.table(enum, opts = OPTS) ⇒ Object

Return a string containing an HTML table representing the data from the enumerable. enum should be an enumerable containing the data. If a block is given, it is yielded each element in enum, and should return an array representing the table data to use for that row.

Options:

:caption

A caption for the table

:column_th

Use th instead of td for the first cell in each row in the body.

:headers

The headers to use for the table, as an array of strings or a single string using commas as the separtor.

: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

An array of widths to use for each column



29
30
31
32
33
34
35
36
# File 'lib/thamble.rb', line 29

def table(enum, opts=OPTS)
  t = Table.new(opts)
  enum.each do |row|
    row = yield(row, t) if block_given?
    t << row
  end
  t.to_s
end