mod_dtcl examples

These are some examples, rather limited ones, of what can be done with mod_dtcl.


Hello world

<+
setcookie "foo" "bar" # we have to put this before any 'hputs' statements

hputs "Hello world"
+>

Produces:

<+ headers setcookie "foo" "bar" # once buffering is switched off, it is no longer possible to # maninuplate headers buffered off hputs "hello world" +>


Conditionals:

<+ if { 1 } { +>

<h2> True </h2>

<+ }  +>

Produces:

<+ if { 1 } { +>

True

<+ } +>

Loops:

<+ 
set x 0 
while { $x < 5 } { 
 hputs "\$x = $x<br>"
 incr x
+>

LOOP<br>

<+ }  +>

Produces:

<+ set x 0 while { $x < 5 } { hputs "\$x = $x
" incr x +> LOOP
<+ } +>


Variables (environmental as well as those passed to the script)


<+ 
 hgetvars
if { [ array exists VARS ] } {
    hputs "< ul>"
    foreach { vr } [ array names VARS ]  {
        hputs "<li>(VARS) $vr = $VARS($vr)"
    }
    hputs "</ul>"
}

if { [ array exists ENVS ] } {
    
    hputs "<ul>"
    foreach { vr } [ array names ENVS ]  {
        hputs "<li>(ENVS) $vr = $ENVS($vr)"
    }
    hputs "</ul>"
}

if { [ array exists COOKIES ] } {
    
    hputs "<ul>"
    foreach { vr } [ array names COOKIES ]  {
        hputs "<li>(COOKIES) $vr = $COOKIES($vr)"
    }
    hputs "</ul>"
}

+>

Produces:

<+ hgetvars if { [ array exists VARS ] } { hputs "

    " foreach { vr } [ array names VARS ] { hputs "
  • (VARS) $vr = $VARS($vr)" } hputs "
" } if { [ array exists ENVS ] } { hputs "
    " foreach { vr } [ array names ENVS ] { hputs "
  • (ENVS) $vr = $ENVS($vr)" } hputs "
" } if { [ array exists COOKIES ] } { hputs "
    " foreach { vr } [ array names COOKIES ] { hputs "
  • (COOKIES) $vr = $COOKIES($vr)" } hputs "
" } +>

Create a table on the fly


<+

set listone { 1 2 3 4 5 6 7 8 }
set listtwo { 1 2 3 4 5 6 7 8 }

hputs "<table>\n"

foreach { a } $listone  { 
    hputs "<tr>\n"
    foreach { b } $listtwo {
	set num [ expr $a * $b * 4 - 1 ]
	hputs [ format "<td bgcolor=%2x%2x%2x >  $num $num $num </td>\n" $num $num $num ]
    }
    hputs "</tr>\n"
}
hputs "</table>\n"
+>


Produces:

<+ set listone { 1 2 3 4 5 6 7 8 } set listtwo { 1 2 3 4 5 6 7 8 } hputs "\n" foreach { a } $listone { hputs "\n" foreach { b } $listtwo { set num [ expr $a * $b * 4 - 1] hputs [ format "\n" $num $num $num ] } hputs "\n" } hputs "
$num $num $num
\n" +>


In addition
There are many, many other things you can do with mod_dtcl. You can, if everything is compiled right, load tcl modules, like libpgtcl.so, so that you can interact with a database!

<+ dtcl_info +>

Return to the mod_dtcl homepage