Cellbrush table generator

Cellbrush is (yet another) PHP library to generate HTML tables.
It does support colspan and rowspan.

What really makes it special is that table cells are addressed by string keys, and empty cells are filled automatically. This means, you can add cells in any order, and you don't need to babysit the table's structural integrity! This allows for a more semantical work flow.

Features

  • Named rows and columns.
  • Colspan and rowspan using col groups and row groups.
  • Automatically fills up empty cells.
  • Method chaining instead of crazy nested array structures.

Example

Here is the code..

<?php
$table
= (new Table())
 
// A 3x3 grid + 'legend' column on the left.
 
->addColNames(['legend', 'col0', 'col1', 'col2'])
  ->
addRowNames(['row0', 'row1', 'row2'])
  ->
td('row0', 'col0', 'Diag 0')
  ->
td('row1', 'col1', 'Diag 1')
  ->
td('row2', 'col2', 'Diag 2')
 
// Add a separator column on the right.
 
->addColName('separator')
  ->
td('', 'separator', '//')
 
// Add colspan group on the right.
 
->addColGroup('cg', ['a', 'b', 'c'])
  ->
td('row0', 'cg.a', 'GD 0.a')
  ->
td('row0', 'cg.b', 'GD 0.b')
  ->
td('row1', 'cg', 'Span')
  ->
td('row2', 'cg.a', 'GD 3.a')
  ->
td('row2', 'cg.c', 'GD 3.c')
;
// Add a thead section.
$table->thead()->addRow('head0')
  ->
th('col0', 'Column 0')
  ->
th('col1', 'Column 1')
  ->
th('col2', 'Column 2')
  ->
th('cg', 'Grouped columns')
;
// Fill the legend cells.
$table->colHandle('legend')
  ->
th('row0', 'Row 0')
  ->
th('row1', 'Row 1')
  ->
th('row2', 'Row 2')
;
print
$table->render();
?>

And this is the generated table:

Column 0Column 1Column 2Grouped columns
Row 0Diag 0//GD 0.aGD 0.b
Row 1Diag 1Span
Row 2Diag 2GD 3.aGD 3.c

Comments

Hallo Andreas,

first, thanks a lot for cellbrush. It looks like well suited for a mini-project with an Intern student.

We want to combine cellbrush with the YAML CSS framework.
And here I got stuck with the CSS Klasses in cellbrush.

  • YAML has a class that can be assigned to a whole table ("bordertable"), but in cellbrush I can only assign classes to individual rows, right? I can also not assign an ID to a table, to use this in a CSS selektor.

  • If I assign the 'bordertable' class to all rows, I don´t find it in the rendered output.
    Here is how I did this: (two header and two data rows, I use this alternating to produce always Description and Value row pairs)

    $table->addRowClass('head1', 'bordertable');
    $table->addRowClass('head2', 'bordertable');
    $table->addRowClass('data1', 'bordertable');
    $table->addRowClass('data2', 'bordertable');

    Many thanks, Vielen Dank!
    Toni

Hi!
I think the README already mentions in "Planned features" that we need ways to add classes and attributes to all html elements. Currently this is lacking.

I have hesitated with this because I was not sure if I should use a 3rd party library for html attribute management, or something baked into Cellbrush.

If you have any ideas about this, I recommend that we continue in the github issue queue.
(But I am traveling atm, so can't promise too much)

Another option is that you simply clone the repo locally or publically, and give it a shot yourself.

Add new comment