Html table
html_table is a custom function that creates HTML tables from arrays of data. It can generate tables with customizable columns, rows, headers, footers, and advanced styling options. All parameters that are not in the list below are printed as name/value-pairs inside the <table> tag.
Attributes
| Attribute Name | Default | Description |
|---|---|---|
| loop | [] | Array of data to loop through |
| cols | 3 | Number of rows in the table. If the rows-attribute is empty, but cols are given, then the number of rows is computed by the number of cols and the number of elements to display to be just enough rows to display all elements. |
| flatten_keys | [] | Specifies the name of columns which exist in loop data. A comma-separated string of column heading names (e.g., "Name, Age, City") or An array of column heading names (e.g., ["Name", "Age", "City"]). |
| rows | null | Number of rows in the table. If the rows-attribute is empty, but cols are given, then the number of rows is computed by the number of cols and the number of elements to display to be just enough rows to display all elements. |
| inner | cols | Direction of consecutive elements in the loop-array to be rendered. cols means elements are displayed col-by-col. rows means elements are displayed row-by-row. |
| caption | null | Text to be used for the <caption> element of the table |
| table_attr | border="1" | Attributes for <table> tag (defaults to 'border="1"') |
| th_attr | [] | Attributes for <th> tag (arrays are cycled) |
| tr_attr | [] | Attributes for <tr> tag (arrays are cycled) |
| td_attr | [] | Attributes for <td> tag (arrays are cycled) |
| trailpad | " " | Value to pad the trailing cells on last row with (if any) (defaults to ' ') |
| hdir | right | Direction of each row to be rendered. possible values: right (left-to-right), and left (right-to-left) (defaults to 'right') |
| vdir | down | Direction of each column to be rendered. possible values: down (top-to-bottom), up (bottom-to-top) (defaults to 'down') |
| tfoot | null | Array of footer values for the last row (e.g., ['Total', 100, 'OK']). Enables <tfoot> generation. |
| tf_attr | [] | Attributes for <td> tags in the footer row (arrays are cycled) |
| col_attr | [] | Array of <col> attributes for column styling. Enables <colgroup> generation. |
| cell_format | null | Callback function for custom cell formatting. Function signature: function($value, $row, $col) |
| colspan | [] | Array of colspan values by [row][col] index. Enables per-cell colspan control. |
| rowspan | [] | Array of rowspan values by [row][col] index. Enables per-cell rowspan control. |
| table_class | "" | CSS class for the table element |
| thead_attr | "" | Attributes for <thead> tag |
| tbody_attr | "" | Attributes for <tbody> tag |
| tfoot_attr | "" | Attributes for <tfoot> tag |
| thead_class | "" | CSS class for thead element |
| tbody_class | "" | CSS class for tbody element |
| tfoot_class | "" | CSS class for tfoot element |
| th_class | "" | CSS class for th elements |
| tr_class | "" | CSS class for tr elements (cycled if space-separated) |
| td_class | "" | CSS class for td elements (cycled if space-separated) |
| tf_class | "" | CSS class for footer td elements |
| tr_class_callback | null | Callback function to determine row class dynamically. Function signature: function($rowIndex, $rowData) |
| td_class_callback | null | Callback function to determine cell class dynamically. Function signature: function($value, $row, $col) |
If you want to generate a table using an associative array, define the
For example, if your data is:
If
flatten_keys names as mentioned above in the table attributes, along with the column names.For example, if your data is:
[
["id" => 10, "name" => "Joe Schmoe"],
["id" => 1001, "name" => "Jack Smith"],
["id" => 1002, "name" => "Jane Johnson"],
["id" => 1003, "name" => "Charlie Brown"],
]
Then set cols as "id, name" for the table headings and flatten_keys as ["id", "name"].If
flatten_keys is defined as ["name", "id"], the data will display name values under the id heading and id values under the name heading.
Examples
Where the template is:
{html_table loop=['A','B','C','D','E','F']}
The above template will output:
<table border="1">
<tbody>
<tr>
<td>A</td><td>B</td><td>C</td>
</tr>
<tr>
<td>D</td><td>E</td><td>F</td>
</tr>
</tbody>
</table>
{* Table with headers and custom styling *}
{html_table
loop=['John','25','Active','Jane','30','Inactive']
cols=['Name','Age','Status']
caption='User Data'
table_class='data-table'
thead_class='table-header'
tbody_class='table-body'
tr_attr=['class="odd"','class="even"']
inner='rows'}
The above template will output:
<table class="data-table" border="1">
<caption>User Data</caption>
<thead class="table-header">
<tr><th>Name</th><th>Age</th><th>Status</th></tr>
</thead>
<tbody class="table-body">
<tr class="odd">
<td>John</td><td>25</td><td>Active</td>
</tr>
<tr class="even">
<td>Jane</td><td>30</td><td>Inactive</td>
</tr>
</tbody>
</table>
Associative Arrays:
<?php
$tplix->assign(
'tabContents',
[
["id"=>10,"name"=>'Joe Schmoe'],
["id"=>1001,"name"=>'Jack Smith'],
["id"=>1002,"name"=>'Jane Johnson'],
["id"=>1003,"name"=>'Charlie Brown'],
]
);
$tplix->assign(
'flatten_keys',["id","name"]
);
{* Table with headers and custom styling *}
{html_table loop=$tabContents cols="ID,Name" flatten_keys=$flatten_keys tr_attr=$tr}
The above template will output:
<table border="1">
<thead>
<tr><th>ID</th><th>Name</th></tr>
</thead>
<tbody>
<tr bgcolor="#eeeeee">
<td>10</td><td>Joe Schmoe</td>
</tr>
<tr bgcolor="#dddddd">
<td>1001</td><td>Jack Smith</td>
</tr>
<tr bgcolor="#eeeeee">
<td>1002</td><td>Jane Johnson</td>
</tr>
<tr bgcolor="#dddddd">
<td>1003</td><td>Charlie Brown</td>
</tr>
</tbody>
</table>
{* Advanced table with headers, footer, custom formatting, and conditional styling *}
{html_table
loop=['John','25','Active','Jane','30','Inactive']
cols=['Name','Age','Status']
tfoot=['Totals','55','2 Users']
col_attr=['style="width: 100px;"','style="width: 50px;"','style="width: 80px;"']
cell_format=$formatter
tr_class_callback=$rowClassCallback
td_class_callback=$cellClassCallback
inner='rows'}
The above template will output a table with:
- Custom column widths via <col> elements
- Header row with <th> elements
- Footer row with <td> elements
- Custom cell formatting applied via callback
- Conditional row classes applied via callback
- Conditional cell classes applied via callback
The
cell_format,tr_class_callback and td_class_callback variables must be assigned to the template scope as callable functions before rendering.