Index: openacs-4/packages/acs-templating/tcl/tag-init.tcl =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/acs-templating/tcl/tag-init.tcl,v diff -u -r1.5 -r1.5.2.1 --- openacs-4/packages/acs-templating/tcl/tag-init.tcl 11 Jul 2002 08:52:26 -0000 1.5 +++ openacs-4/packages/acs-templating/tcl/tag-init.tcl 17 Feb 2003 00:11:02 -0000 1.5.2.1 @@ -550,3 +550,151 @@ } " } + +# DanW: implements a switch statement just like in tcl +# use as follows: +# +# +# +# Foo, Bar or Baz was selected +# +# +# A was selected +# +# +# B was selected +# +# +# C was selected +# +# +# Not a valid selection +# +# +# +# The flag switch is optional and it defaults to exact if not specified. +# Valid values are exact, regexp, and glob + +template_tag switch { chunk params } { + + set sw "" + set arg "" + set size [ns_set size $params] + + # get the switch flags and the switch var + + for { set i 0 } { $i < $size } { incr i } { + set key [ns_set key $params $i] + set value [ns_set value $params $i] + + if { [string equal $key $value] } { + set arg $key + } elseif [string equal $key flag] { + append sw " -$value " + } + } + + # append the switch statement and eval tags in between + + template::adp_append_code "switch $sw -- $arg {" + + template::adp_compile_chunk $chunk + + template::adp_append_code "}" +} + + +# case statements as part of switch statement as shown above +# + +template_tag case { chunk params } { + + # Scan the parameter stack backward, looking for the tag name + + set tag_id [template::enclosing_tag switch] + if { [string equal $tag_id {}] } { + error "No enclosing SWITCH tag for CASE tag on value $value" + } + + # get the case value + + set value [ns_set iget $params value] + + # insert the case statement and eval the chunk in between + + if ![string equal $value ""] { + + # processing form + + template::adp_append_code "$value {" -nobreak + + template::adp_compile_chunk $chunk + + template::adp_append_code "}" + + } else { + + # processing form + + set switches "" + set size [ns_set size $params] + set size_1 [expr $size - 1] + + for { set i 0 } { $i < $size } { incr i } { + + set key [ns_set key $params $i] + set value [ns_set value $params $i] + + # pass over the first arg (syntax sugar), but check format + if { $i == 0 } { + + if ![string equal $key "in"] { + error "Format error: should be " + } + + } else { + + if { [string equal $key $value] } { + + # last item in list so process the chunk + if { $i == $size_1 } { + + template::adp_append_code "$switches $value {" -nobreak + + template::adp_compile_chunk $chunk + + template::adp_append_code "}" + + } else { + + # previous items default to pass-through + append switches " $key - " + } + + } else { + error "Format error: should be " + } + } + } + } +} + +# default value for case statement which is a sub-tag in switch tag + +template_tag default { chunk params } { + + # Scan the parameter stack backward, looking for the tag name + + set tag_id [template::enclosing_tag switch] + if { [string equal $tag_id {}] } { + error "No enclosing SWITCH tag for DEFAULT tag" + } + + # insert the default value and evaluate the chunk + + template::adp_append_code "default {" -nobreak + + template::adp_compile_chunk $chunk + + template::adp_append_code "}" +}