<?lsmb- BLOCK dynatable;

TOTAL_WIDTH=14; # cm. using A4 as a basis because it is slightly narrower than
                # US Letter. This way the dynatable works for both paper sizes.
                # This assumes a 1cm margin on either side. --CT
DECLARED_WIDTH=0; 

FOREACH COL IN columns;
    DECLARED_WIDTH = DECLARED_WIDTH + COL.pwidth; # pwidth is arbitrary scale
END; 

IF DECLARED_WIDTH > 0;
    WIDTH_PER_P = TOTAL_WIDTH / DECLARED_WIDTH;
ELSE;
    WIDTH_PER_P = 1;
END;
 ?>
\begin{longtable}{<?lsmb-

FOREACH COL IN columns;
   IF COL.psep_before;
      '|';
   END;
   IF COL.pwidth;
       "p{" _ (COL.pwidth * WIDTH_PER_P) _ "cm}";
   ELSIF COL.palign;
        COL.palign;
   ELSE;
        'l';
   END;
   IF COL.psep_after;
      '|';
   END;
END; 
-?>}
<?lsmb IF firsthead; firsthead ?>\\
<?lsmb- END ?>
<?lsmb IF head; head ?>\\<?lsmb- END ?>
<?lsmb- -?>
<?lsmb 
SKIP_TYPES = ['hidden', 'radio', 'checkbox'];

ADD_SEP = 0;
FOREACH COL IN columns;
    IF ADD_SEP;
      ' & ';
    END;
    ADD_SEP = 1;
    IF 0 == SKIP_TYPES.grep(COL.type).size();
        COL.name;
    END;
END;
-?>\\
\hline\hline
\endfirsthead
<?lsmb 
SKIP_TYPES = ['hidden', 'radio', 'checkbox'];

ADD_SEP = 0;
FOREACH COL IN columns;
    IF ADD_SEP;
      ' & ';
    END;
    ADD_SEP = 1;
    IF 0 == SKIP_TYPES.grep(COL.type).size();
        COL.name;
    END;
END;
-?>\\
\hline\hline
\endhead
<?lsmb

FOREACH ROW IN tbody.rows;
    ADD_SEP = 0;
    FOREACH COL IN columns;
        IF ADD_SEP;
           ' & ';
        END;
        ADD_SEP = 1;
        COL_ID = COL.col_id;
        IF 0 == SKIP_TYPES.grep(COL.type).size();
            ROW.$COL_ID; #$
        END;
    END; 
    ?>\\
<?lsmb
END; # FOREACH ?>
\end{longtable}
<?lsmb END;  # block dynatable -?>
