A little layout manager for html forms

MHo The layout manager procedure:

 proc grid {grid {colgroup {"" ""}} args} {
      set cols [llength $colgroup]
      append ret "<table [eval nameValParms $args]>\n"
      append ret " <colgroup>\n"
      foreach g $colgroup {
         append ret "  <col [eval nameValParms $g]>\n"
      }
      append ret " </colgroup>\n"
      foreach row $grid {
         set r [string trim $row]
         lassign [split $r |] r p
         # Problem: woher sollen nun die <tr args> kommen?
         append ret " <tr [eval nameValParms $p]>\n"
         if {[string eq $r "----"]} {
            append ret "  <td colspan=\"$cols\"><hr></td>"
         } elseif {[string length $r] == 0} {
            # leere Zeilen machen Sinn, weil tr ein Attribut tragen kann (z.B. Höhe)
            append ret "  <td colspan=\"$cols\"></td>";# &nbsp; oder nicht?
         } else {
            append ret "  "
            set i 0
            foreach col $r {
               incr i
               set c [string trim $col]
               lassign [split $c §] c p
               if {[string eq $c "-"]} {
                  append ret "<td [eval nameValParms $p]>&nbsp;</td>"
               } else {
                  append ret "<td [eval nameValParms $p]>[subst $c]</td>"
               }
            }
            if {$i < $cols} {
               # fehlende Spalten ohne Parms ergänzen
               append ret [string repeat "<td>&nbsp;</td>" [expr {$cols-$i}]]
            }
         }
         append ret "\n </tr>\n"
      }
      append ret "</table>\n"; # Ergebnis wird automatisch zurückgegeben
 }

A helper proc:

 proc nameValParms {args} {
      set ret ""
      foreach {name val} $args {
         append ret "$name=\"$val\" "
      }
      return $ret
 }

An example:

 puts [grid {
    { -  1b 1c |bgcolor grey}
    { 2a -  {2c§rowspan 2} }
    {          |height 30}
    { ----     }
    { 3a 3b    }
 } {"width 20%" "width 40%" "width 40%"} border 1 width 40%] 

Html generated by example:

 <table border="1" width="40%" >
  <colgroup>
   <col width="20%" >
   <col width="40%" >
   <col width="40%" >
  </colgroup>
  <tr bgcolor="grey" >
   <td >&nbsp;</td><td >1b</td><td >1c</td>
  </tr>
  <tr >
   <td >2a</td><td >&nbsp;</td><td rowspan="2" >2c</td>
  </tr>
  <tr height="30" >
   <td colspan="3"></td>
  </tr>
  <tr >
   <td colspan="3"><hr></td>
  </tr>
  <tr >
   <td >3a</td><td >3b</td><td>&nbsp;</td>
  </tr>
 </table>

Of course, this needs to be commented...