{tr-alist} {td-alist} args] which would use the [join] trickery below to generate a table row. [[- ]] [[
- ]] would work similarly.
----
== Armouring HTML ==
Prevent output from containing html-significant characters
interp alias {} armour {} string map {& & < < > > \" " ' '} ... fairly clearly ''that'' needs some work :)
----
== iteration ==
Often what you want isn't to iterate, but to convert a list into something else. One generic technique I've found helpful is the use of [join], as follows:
]
Which will convert a tcl list into an HTML unordered list. Similar techniques are possible with s
The technique can be readily made generic.
== [subst] instead of [eval] ==
[jcw]'s comment (on [TemplaTcl: a Tcl template engine]) that the problem with [subst] is that it requires awkward double-quoting lead to a realization that some of the common tcl commands which provide a body argument ([if], [while], [foreach], [for], [switch]) could be modified (in tcl) to use [subst] to evaluate the body using [subst] instead of [eval]
== Subst-if ==
Here's a variant of [if] which evaluates its conditional using [subst] and not [eval]
proc If {args} {
while {[llength $args] && ![uplevel expr [lindex $args 0]]} {
set args [lrange $args 2 end] ;# lose the cond and positive-cond
if {[lindex $args 0] eq "else"} {
break
}
set args [lrange $args 1 end] ;# assumed to be 'elseif'
}
return [uplevel subst [list [lindex $args 1]]] ;# return with neg-consequence
}
set a 1; set b 2
puts [If {$a} {a is true} else {a is false}]
puts [If {$a == $b} {a == b} elseif {$b == 1} {b is 1} else {b is not 1 it is $b}]
Note that this version does not support the ''then'' keyword in [if], and requires the ''else'' keyword. This suits my style - it should be able to be modified if it doesn't suit yours.
== Subst-while ==
proc While {cond body} {
set result {}
while {[uplevel $cond]} {
lappend result [uplevel subst [list $body]]
}
return $result
}
''[jcw] - We'll have to agree to disagree here, Colin. You solve the problem by adding N alternative implementations, whereas I choose to solve it once with a pre-processing step.''
[CMcC] I didn't mean to imply that you endorsed this direction of solution, merely that you had succinctly posed the problem. In considering the problem being solved by templating systems in light of some of the reasons given on that page, it occurred to me that the problem of adoption and usage by non-tcl people was not with tcl syntax per se (compared with the alternative offered,) but was as much with the superfluity of commands available, and with their lack of task-specificity. I therefore consider providing a bunch of functions which may apply to the specific domain to be an acceptable approach.
|