'''Purpose''' [DDG] After seeing a lot of template approaches where the template becomes more and more bloated with programming logic I was reading the interesting article at www.perl.com [http://www.perl.com/pub/a/2008/03/14/reverse-callback-templating.html] about Template::Recall by James Robson. Here is it's Tcl-counterpart written as an [Itcl]-class: ---- ====== # Created By : Dr. Detlef Groth # Created : Mon Mar 17 13:56:04 2008 # # Description # # History # package require Itcl itcl::class TemplateRecall { public variable template_path "" private variable TemplateText "" private variable Templ constructor {args} { eval configure $args if {$template_path eq ""} { error "Option -template_path not given" } if [catch {open $template_path r} infh] { puts stderr "Cannot open $template_path: $infh" exit } else { set TemplateText [read $infh] close $infh } array set Templ [list] } method render {template args} { if {![info exists Templ($template)]} { foreach line [split $TemplateText "\n"] { if {[regexp {\[ *=+ ([^\s]+) +=+ *\]} $line -> lastTempl]} { continue } elseif {[info exists lastTempl]} { append Templ($lastTempl) "$line\n" } } } set cpTempl $Templ($template) set cpTempl [regsub -all {\['} $cpTempl "'''''"] set cpTempl [regsub -all {']} $cpTempl "'''''"] foreach {var repl} $args { set var [regsub {^-} $var ""] set cpTempl [regsub -all "'{5} +$var +'{5}" $cpTempl $repl] } return $cpTempl } } if {0} { set goods [list "oxfords,Brown leather,\$85,0" \ "hiking,All sizes,\$55,7" \ "tennis shoes,Women's sizes,\$35,15" \ "flip flops,Colors of the rainbow,\$7,90" ] TemplateRecall tr -template_path template1.html puts [tr render header -title MyStore -date [clock format [clock seconds]]] foreach good $goods { set attr [split $good ,] set q [lindex $attr 3] if { $q == 0} { set q "Out Of stock" } puts [tr render product_row -shoe [lindex $attr 0] -details [lindex $attr 1] \ -price [lindex $attr 2] -quantity $q] } puts [tr render footer] ====== ---- Here is the template-file template1.html: [ =================== header ===================] my site - [' title ']

The date is [' date ']

[ =================== product_row =================== ] [= footer =]
Shoe Details Price Quantity
[' shoe '] [' details '] [' price '] [' quantity ']
---- Here is the output of the test: my site - MyStore

The date is Mon Mar 17 15:23:45 +0100 2008

Shoe Details Price Quantity
oxfords Brown leather $85 Out Of stock
hiking All sizes $55 7
tennis shoes Women's sizes $35 15
flip flops Colors of the rainbow $7 90
'''Discussion''' Enter your comments here. <> Template