Use tables to implement <table>, <tr>, <th>, <td>, <thead>, <tbody> and <tfoot> tags.
Parameters
<table>, <tr>, <thead>, <tbody> and <tfoot> can take 2 optional configuration params. <th> and <td> tags can also take a third param, text input.
id (optional)
Expects a string with all ids the element should have.
class (optional)
Expects a string with all classes the element should have.
text (optional)
Expects a string which will be rendered between the opening and closing <th> or <td>-tag
Example 1
Implementing a simple, hard coded table.
table class: "foo" do
thead do
tr class: "bar" do
th text: "First"
th text: "Matestack"
th text: "Table"
end
end
tbody do
tr do
td text: "One"
td text: "Two"
td text: "Three"
end
tr do
td text: "Uno"
td text: "Dos"
td text: "Tres"
end
end
tfoot do
tr do
td text: "Eins"
td text: "Zwei"
td text: "Drei"
end
end
end
The real beauty comes into play when things get a little more complicated
def prepare
@users = ["Jonas", "Pascal", "Chris"]
@numbers = ["One", "Two", "Three"]
@numeros = ["Uno", "Dos", "Tres"]
end
# ...
table class: "foo" do
tr class: "bar" do
@users.each do |user|
th text: user
end
end
tr do
@numbers.each do |number|
td text: number
end
end
tr do
@numeros.each do |numero|
td text: numero
end
end
# you are still in pure Ruby, so feel free to do other stuff as well
tr do
td do
plain "Do"
end
td text: "Custom"
td do
plain "Stuff"
end
end
end
thead, tbody and tfoot are optional containers for any number of trs. If none are specified, tbody will be used to contain all tr components. thead is typically used for the head of the table, and tfoot for any table footer, where applicable, such as a sum or count.
table do
thead do
tr do
th text: "Product"
th text: "Price"
end
end
# tbody is unnecessary, since it has no class or id and will be added automatically
# tbody do
tr do
td text: "Apples"
td text: "3.50"
end
tr do
td text: "Oranges"
td text: "2.75"
end
tr do
td text: "Bananas"
td text: "4.99"
end
# end
tfoot do
tr do
td text: "Total:"
td text: "11.24"
end
end
end