Template:Colevel/doc

Purpose
is a set of templates which allow a list of single items or a list of coupled items to be displayed in one or more columns and be automtically column depth balanced.

The templates are not designed to be called directly from an article, but are designed to be incorporated into other templates which are presenting tables of many items.

There are three entry point templates:
 * Item set produces a row for a table consisting of one or more "columns" of items or coupled items. It must always be enclosed within a table declaration.
 * This is used or linked to by pages
 * This is used or linked to by pages


 * Find first returns the positional number of the first instantiated item in an optionally populated list of items. If there are no items it returns 31.
 * This is used or linked to by pages
 * This is used or linked to by pages


 * Find last returns the positional number of the last set item in an optionally populated list of items. If there are no items it returns 0.
 * This is used or linked to by pages
 * This is used or linked to by pages


 * Count params returns the number of the set parameters in an optionally populated list of items.
 * This is used or linked to by pages
 * This is used or linked to by pages


 * Count items returns the number of set items in an optionally populated list of items, between and including a start item and an end item.
 * This is used or linked to by pages
 * This is used or linked to by pages

See templates and  for typical applications.

Rationale
Often articles have tables of many items spread over multiple columns, and an editor has spent a non trivial amount of time getting the columns balanced and the table looking good.

One more item or several items are then required to be inserted in to the table, requiring the editing of the whole table to move items around and across and down columns. Or, the number of columns in the table needs to be changed to either allow it to better fit into the article or as more and more values are added. Not all editors are as good at editing tables as others and mistakes are made, or the potential content editor is put off altogether, or editors avoid constructing the table in the first place because it will be difficult to maintain.

These templates allow other templates which generate tables to be constructed to allow the table to be kept in the article as a simple list of items, allowing editors to be concerned only with content and article layout, and not with either table syntax or laborious table reconfiguration as the article grows.

Number of items
The maximum number of single items or number of coupled items is 30, that is for coupled items a total of 60 items.

Column balancing
Column balancing does not allow for inconsistent text wrap of items, and text wrap can disturb column depth balancing.

If there is an equivalent number of text wrap new lines in each column then the columns will remain balanced.

Missing items
Items missing from within the list of items are treated as items and a blank position will be displayed in the column where the missing item would otherwise appear.

Usage
The main entry point template is

Skeleton
Item set uses named parameters:
 * alpha1, optional, text for an item, or for the first item of two coupled items, in a list of up to 30 items, optionally linked text
 * beta1, optional, text for the second item of two coupled items, optionally linked text
 * firstitem, mandatory, the positional number of the first item to be displayed from the list, number from 1 to 30
 * lastitem, mandatory, the positional number of the last item to be displayed from the list, number from 1 to 30
 * columns, mandatory, the number of columns to be displayed, number from 1
 * alphawidth, mandatory, the percentage width for the first item of two coupled items. If 100 then only the first item in the couple is displayed.  If 0 then only the second item in the couple is displayed, number from 0 to 100

Skeleton
Find first uses positional parameters:
 * 1, optional, an item in a list, up to 30, any content

Skeleton
Find last uses positional parameters:
 * 1, optional, an item in a list, up to 30, any content

Skeleton
Count params uses positional parameters:
 * 1, optional, an item in a list, up to 30, any content

Skeleton
Count item uses positional parameters and named parameters:
 * 1, optional, an item in a list, up to 30, any content
 * firstitem, mandatory, items with a position before firstitem are ignored, number
 * lastitem, mandatory, items with a position after lastitem are ignored, number

Full list
First item is:

List with gaps
First item is:

Empty list
First item is:

Full list
Last item is:

List with gaps
Last item is:

Empty list
Last item is:

Full list
Item count is:

List with gaps
Item count is:

Empty list
Item count is:

Full list
Paameter count is:

List with gaps
Parameter count is:

Empty list
Parameter count is:

Notes for template editors
A "column" in the row produced by the template is actually a group of items or coupled items with each item placed on a new line.

Algorithm
The algorithms for this set of templates are:

Item set
The main challenge to be overcome to achieve depth blanced columns is that HTML tables and hence Wiki tables are "row major", that is rows are defined, not columns; the table is constructed row by row. Cells in rows are then aligned to make cells look like they are in columns.

This alogorithm emulates a rudimentrary "column major" table column by column and balances the depth of the columns so that no column is more than one item shorter than any other column, that is when there is a short fall of more than one item, instead of the last column being shorter than all other columns, the shortfall is evened out over multiple columns.

A "column" is a cell in a row with items separated by line breaks to emulate a column. numberofitems := lastitem - firstitem + 1 rows := 1 + (numberofitems - 1 ) intdiv columns fullcolumns := if numberofitems mod columns = 0 then columns else numberofitems mod columns itemsinfullcolumns := fullcolumns x rows start a row of columns for item := firstitem to lastitem eachitem := item - firstitem + 1 if eachitem <= itemsinfullcolumns then if rows == 1 then start column display list(item) else itemmodrows := eachitem mod rows if itemmodrows == 1 then start column display list(item) continue column else display list(item) if itemmodrows != 0 then continue column else if (rows - 1) == 1 then start column display list(item) else itemmodrows := (eachitem - itemsinfullcolumns) mod (rows - 1) if itemmodrows == 1 then start column display list(item) continue column else display list(item) if itemmodrows != 0 then continue column

Other templates within colevel




then start column display item else if itemmodrows == 1 then start column display item continue column else display item if itemmodrows != 0 then continue column 



Find first
item := 1 while item <= maxitem and list(item) is empty item := item + 1 firstitem := item

Find last
lastitem := maxitem + 1 - firstitem( reverse( list ) )

Structure
Two key factors have influenced the way the algorithms have been structured into templates:
 * 1) There is no looping or recursion available. This has required a loop to be simulated by expressing each iteration of the loop as a separate but incremented instantantion of the loop control and content.
 * 2) To reduce server load a bit, calculated values which are required multiple times within a template (module) (and some module as are called for evey item) and would otherwise be calculated there once and stored as a working variable, are calculated once by a template calling the template and passed as a parameter.

To increase number of items
The current maximum number of items is 30.

To increase the maximum number of items from 31 to MaxItem the following need to be done:
 * Template:Colevel/item set
 * Update the last item check from 31 to MaxItem + 1.
 * Add parameter passing from 31 to MaxItem.


 * Template:Colevel/all items
 * Add loop instantiations for each value from 31 to MaxItem.


 * Template:Colevel/find first
 * Add loop instantiations for each value from 31 to MaxItem.


 * Template:Colevel/find last
 * Update 31 to Maxitem + 1.
 * Add parameter passing from 31 to MaxItem.


 * Update this documentation wherever 30 or 31 appears to be MaxItem and MaxItem + 1 respectively.