create new tag
, view all tags
I propose we extend the table syntax so that one can:
  • add table options (bgcolor, bgimage, border ...)
  • define the alignment of columns (left, right, centered)

(Who wants full control should use normal HTML table tags)


	| l | r | c | r | border="3" bgcolor="#C0FFC0" width="90%"
	| left aligned text | right aligned text | center aligned text | right aligned text |
	| l | r | c | r |
Will produce:
left aligned text right aligned text center aligned text right aligned text
l r c r

The first line is distinguished from a first table line because:

  • each cell contains only a char in {lcr}
  • after last "pipe" char there is optional text (the table options)

Here is a re-write of the emitTR subroutine that does the above:

# =========================
#AS 1 Nov 2000 added column definitions and table options
use vars qw( @colOptions 
		 $colDefPattern );
$colDefPattern = qr/^((\s*[clr]\s*\|)+)([^|]*)$/io ;
sub emitTR {
	 my ( $pre, $cells, $insideTABLE ) = @_;
	 my $colOptions = "";
	 if( $insideTABLE ) {
		  $cells = "|$cells";
	 } else {
	@colOptions = ('align="left"');
	if ( $cells =~ m/$colDefPattern/ ) {
		 $cells = "";
		 $pre .= "<TABLE $3>";
		 my $colOptions = $1;
		 $colOptions =~ s/\s*c\s*\|/align="center"|/gio;
		 $colOptions =~ s/\s*l\s*\|/align="left"|/gio;
		 $colOptions =~ s/\s*r\s*\|/align="right"|/gio;
		 @colOptions = split( /\s*\|\s*/, $colOptions );
	} else {
		 $pre .= "<TABLE border=\"1\">";
			  $cells = "|$cells";
	 my ($fop, @rops) = @colOptions;
	 $cells =~ s@\|$@</TD></TR>@o;
	 $cells =~ s@^\|@<TR><TD $fop>@o;
	 my $opt = "";
	 foreach $opt (@rops)
	$cells =~ s@\|@</TD><TD $opt>@o;	
	 $cells =~ s@\|@</TD><TD>@go;
	 return $pre . $cells;


  • check the syntax of table options
  • extend the syntax of first line to specify:
    • the column colour
    • the column width
  • recognize bold text in cells and transform it to <TH>.
| *header text* | *header text* | *header text* |
| *header text* | text			 | text			 |
header textheader textheader text
header texttexttext

-- AndreaSterbini - 02 Nov 2000

I have forgot to mention that the lines in wiki.pm where emitTR is called should be modified as follows:

#AS 1 Nov 2000 accept table options in first line
#				if( $_ =~ /^(\s*)\|.*\|$/ ) {
				if( $_ =~ /^(\s*)\|.*\|.*$/ ) {

-- AndreaSterbini - 07 Nov 2000

I like the idea to extend the table syntax. However I would prefer not using a leading "| .. | .. |" line to define the table attributes, IMHO this is not so intuitive. How about using the TWiki %VARIABLE% syntax instead? Idea: The %TABLE% variable defines the table attributes, i.e.

%TABLE{border="3" bgcolor="#C0FFC0" width="90%"}%

These values remain active for all subsequent "| .. | .. |" tables until redefined or undefined (with empty %TABLE% ).

The same syntax could be used for individual columns or cells. Example:

%TABLECOLUMN{align="left" bgcolor="yellow" width="30%"}%

Also these values remain active for all subsequent columns until redefined or undefined (with empty %TABLECOLUMN% ).

Here is the new syntax for the table example you used above: (with last column cut, and first and second row reversed)

	%TABLE{border="3" bgcolor="#C0FFC0" width="90%"}%=

	| %TABLECOLUMN{align="left"}% l | %TABLECOLUMN{align="right"}% r | %TABLECOLUMN{align="center"}% c |
	| left aligned text | right aligned text | center aligned text |

Or, how about defining all table attributes outside the table instead? This would make it more readable. Same table example, with individual row bgcolors:

	%TABLE{border="3" width="90%"}%=
	%TABLECOL1{align="left" bgcolor="yellow"}% 
	%TABLECOL2{align="right" bgcolor="magenta"}% 
	%TABLECOL3{align="center" bgcolor="green"}% 

	| l | r | c |
	| left aligned text | right aligned text | center aligned text |

-- PeterThoeny - 25 Nov 2000

We are probably re-inventing the TABLE tag, I think it's better to use the original one. (We must keep to the minimalistic WikiZen and avoid CreepingFeaturism ... )

Has somebody suggestions to extend the table syntax and KISS? (keep it simple, stupid smile )

-- AndreaSterbini - 29 Nov 2000

Yes, at least for horizontal alignment. For right and left alignment, use extra spaces in the item, Two or more spaces at the left mean right-alignment, two or more spaces at the right mean left-alignment (the default, anyway), and two or more spaces at both sides say to centre it. If you are trying to type your table so it looks right while editing, you will probably find yourself typing these spaces a lot of the time anyway.

-- HendrikBoom - 18 Mar 2001

One thing I desperately need, though is COLSPAN. We're working on net protocols, and we frequently need diagrams for packet layouts. We use tables for this. The top row of one of these tables is a ruler of bit counts, and subsequent lines contain the fields. Trouble is, fields are often more than one bit wide! At the start of the table, too, there a sequence of COL directives to tell it that all the columns should have the same width.

So to make things at all feasible, I introduces a change in by table formatting code.

  • (first try) use multiple consecutive bars to indicate a colspan greater than one (thus |||| would introduce a field that is four columns wide). But this looked ugly, and the columns didn't line up reasonably while editing.
  • (second try) multiple consecutive bars again, but this time, after the entry. More readable, but counting bars is a pain.
  • (third try) insert an integer immediately after the bar that introduces an item. This |4 hex digit ! as an entry indicates an entry that is four columns wide.

I edited this into my production TWiki, but hated doing it because it potentially introduces severe incompatibility with existing tables and with other TWikis. So far, though, I have had no problems with it.

Does anyone have better ideas how to achieve this?

And I have found no intuitive notation to indicate that all the columns are to be the same width.

-- HendrikBoom - 18 Mar 2001

I extended TWiki's table handling based on Hendrik's ideas:

Header is rendered as colored TH tag
left left left
center center center
right right right
colspan 2 text
colspan 3
back to normal header anywhere

  • Any cell that looks like | *text* | is rendered as TH tag with colored background.
  • To center align a cell use two or more spaces on both sides
  • To right align a cell use two or more spaces on the left side, and zero to one space on the right side.
  • Colspan is defined as multiple consecutive bars, after the entry. (Hendrik's second try)

I believe this enhancment should not have any unwanted side effects with existing text. Let us know if you find one.

Commited changes to TWikiAlphaRelease.

-- PeterThoeny - 27 Mar 2001


-- AndreaSterbini - 27 Mar 2001


-- JohnTalintyre - 28 Mar 2001

Edit | Attach | Watch | Print version | History: r9 < r8 < r7 < r6 < r5 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r9 - 2001-04-27 - KlausWriessnegger
  • Learn about TWiki  
  • Download TWiki
This site is powered by the TWiki collaboration platform Powered by Perl Hosted by OICcam.com Ideas, requests, problems regarding TWiki? Send feedback. Ask community in the support forum.
Copyright © 1999-2017 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.