{
    "mode": "perldoc",
    "parameter": "Template::Plugin::Table",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/Template%3A%3APlugin%3A%3ATable/json",
    "generated": "2026-07-05T13:37:55Z",
    "synopsis": "[% USE table(list, rows=n, cols=n, overlap=n, pad=0) %]\n[% FOREACH item IN table.row(n) %]\n[% item %]\n[% END %]\n[% FOREACH item IN table.col(n) %]\n[% item %]\n[% END %]\n[% FOREACH row IN table.rows %]\n[% FOREACH item IN row %]\n[% item %]\n[% END %]\n[% END %]\n[% FOREACH col IN table.cols %]\n[% col.first %] - [% col.last %] ([% col.size %] entries)\n[% END %]",
    "sections": {
        "NAME": {
            "content": "Template::Plugin::Table - Plugin to present data in a table\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "[% USE table(list, rows=n, cols=n, overlap=n, pad=0) %]\n\n[% FOREACH item IN table.row(n) %]\n[% item %]\n[% END %]\n\n[% FOREACH item IN table.col(n) %]\n[% item %]\n[% END %]\n\n[% FOREACH row IN table.rows %]\n[% FOREACH item IN row %]\n[% item %]\n[% END %]\n[% END %]\n\n[% FOREACH col IN table.cols %]\n[% col.first %] - [% col.last %] ([% col.size %] entries)\n[% END %]\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "The \"Table\" plugin allows you to format a list of data items into a virtual table. When you\ncreate a \"Table\" plugin via the \"USE\" directive, simply pass a list reference as the first\nparameter and then specify a fixed number of rows or columns.\n\n[% USE Table(list, rows=5) %]\n[% USE table(list, cols=5) %]\n\nThe \"Table\" plugin name can also be specified in lower case as shown in the second example\nabove. You can also specify an alternative variable name for the plugin as per regular Template\nToolkit syntax.\n\n[% USE mydata = table(list, rows=5) %]\n\nThe plugin then presents a table based view on the data set. The data isn't actually reorganised\nin any way but is available via the \"row()\", \"col()\", \"rows()\" and \"cols()\" as if formatted into\na simple two dimensional table of \"n\" rows x \"n\" columns.\n\nSo if we had a sample \"alphabet\" list contained the letters '\"a\"' to '\"z\"', the above \"USE\"\ndirectives would create plugins that represented the following views of the alphabet.\n\n[% USE table(alphabet, ... %]\n\nrows=5                  cols=5\na  f  k  p  u  z        a  g  m  s  y\nb  g  l  q  v           b  h  n  t  z\nc  h  m  r  w           c  i  o  u\nd  i  n  s  x           d  j  p  v\ne  j  o  t  y           e  k  q  w\nf  l  r  x\n\nWe can request a particular row or column using the \"row()\" and \"col()\" methods.\n\n[% USE table(alphabet, rows=5) %]\n[% FOREACH item = table.row(0) %]\n# [% item %] set to each of [ a f k p u z ] in turn\n[% END %]\n\n[% FOREACH item = table.col(2) %]\n# [% item %] set to each of [ m n o p q r ] in turn\n[% END %]\n\nData in rows is returned from left to right, columns from top to bottom. The first row/column is\n0. By default, rows or columns that contain empty values will be padded with the undefined value\nto fill it to the same size as all other rows or columns.\n\nFor example, the last row (row 4) in the first example would contain the values \"[ e j o t y\nundef ]\". The Template Toolkit will safely accept these undefined values and print a empty\nstring. You can also use the IF directive to test if the value is set.\n\n[% FOREACH item = table.row(4) %]\n[% IF item %]\nItem: [% item %]\n[% END %]\n[% END %]\n\nYou can explicitly disable the \"pad\" option when creating the plugin to returned shortened\nrows/columns where the data is empty.\n\n[% USE table(alphabet, cols=5, pad=0) %]\n[% FOREACH item = table.col(4) %]\n# [% item %] set to each of 'y z'\n[% END %]\n\nThe \"rows()\" method returns all rows/columns in the table as a reference to a list of rows\n(themselves list references). The \"row()\" methods when called without any arguments calls\n\"rows()\" to return all rows in the table.\n\nDitto for \"cols()\" and \"col()\".\n\n[% USE table(alphabet, cols=5) %]\n[% FOREACH row = table.rows %]\n[% FOREACH item = row %]\n[% item %]\n[% END %]\n[% END %]\n\nThe Template Toolkit provides the \"first\", \"last\" and \"size\" virtual methods that can be called\non list references to return the first/last entry or the number of entries in a list. The\nfollowing example shows how we might use this to provide an alphabetical index split into 3 even\nparts.\n\n[% USE table(alphabet, cols=3, pad=0) %]\n[% FOREACH group = table.col %]\n[ [% group.first %] - [% group.last %] ([% group.size %] letters) ]\n[% END %]\n\nThis produces the following output:\n\n[ a - i (9 letters) ]\n[ j - r (9 letters) ]\n[ s - z (8 letters) ]\n\nWe can also use the general purpose \"join\" virtual method which joins the items of the list\nusing the connecting string specified.\n\n[% USE table(alphabet, cols=5) %]\n[% FOREACH row = table.rows %]\n[% row.join(' - ') %]\n[% END %]\n\nData in the table is ordered downwards rather than across but can easily be transformed on\noutput. For example, to format our data in 5 columns with data ordered across rather than down,\nwe specify \"rows=5\" to order the data as such:\n\na  f  .  .\nb  g  .\nc  h\nd  i\ne  j\n\nand then iterate down through each column (a-e, f-j, etc.) printing the data across.\n\na  b  c  d  e\nf  g  h  i  j\n.  .\n.\n\nExample code to do so would be much like the following:\n\n[% USE table(alphabet, rows=3) %]\n[% FOREACH cols = table.cols %]\n[% FOREACH item = cols %]\n[% item %]\n[% END %]\n[% END %]\n\nOutput:\n\na  b  c\nd  e  f\ng  h  i\nj  .  .\n.\n\nIn addition to a list reference, the \"Table\" plugin constructor may be passed a reference to a\nTemplate::Iterator object or subclass thereof. The Template::Iterator getall() method is first\ncalled on the iterator to return all remaining items. These are then available via the usual\nTable interface.\n\n[% USE DBI(dsn,user,pass) -%]\n\n# query() returns an iterator\n[% results = DBI.query('SELECT * FROM alphabet ORDER BY letter') %]\n\n# pass into Table plugin\n[% USE table(results, rows=8 overlap=1 pad=0) -%]\n\n[% FOREACH row = table.cols -%]\n[% row.first.letter %] - [% row.last.letter %]:\n[% row.join(', ') %]\n[% END %]\n",
            "subsections": []
        },
        "AUTHOR": {
            "content": "Andy Wardley <abw@wardley.org> <http://wardley.org/>\n",
            "subsections": []
        },
        "COPYRIGHT": {
            "content": "Copyright (C) 1996-2007 Andy Wardley. All Rights Reserved.\n\nThis module is free software; you can redistribute it and/or modify it under the same terms as\nPerl itself.\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "Template::Plugin\n",
            "subsections": []
        }
    },
    "summary": "Template::Plugin::Table - Plugin to present data in a table",
    "flags": [],
    "examples": [],
    "see_also": []
}