{tr-alist} {td-alist} args] which would use the [join] trickery below to generate a table row. [[- ]] [[
- ]] would work similarly.
''[escargo] 3 Jun 2007'' - Pardon my density, but does this mean that [[
- ]] would put
- in the output buffer?
That's what ''I'' would expect it to do. What do ''you'' expect it to do? And I don't understand the { ! "Home" }
notion at all. I presume ! must be shorthand for something, but It's not clear to me what. It can't be negation. Maybe some kind of data reference?
[CMcC] 'output buffer' is more than somewhat server dependent. What it does is return a properly formatted string suitable for delivery as part of an HTML page.
[escargo] How would this work for cases like (admittedly both limited and contrived.
Forbidden Planet |
[CMcC] something like this:
[ [ [ nowrap 1 align center class first id monster [ "Forbidden Planet"]]]]
would produce a rough equivalent of your example.
Of course, if you stored your table props in an array or dict it'd be even better:
[ [ [ {*}$attrs id monster [ "Forbidden Planet"]]]]
''[escargo]'' - So one of the disadvantages is needing to quote strings ("Forbidden Planet")
that would not need to be quoted in HTML?
[CMcC] in your example, the string must have been implicitly quoted.
----
== 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
}
== Subst-foreach ==
proc Foreach {args} {
set body [lindex $args end]
set vars [lrange $args 0 end-1]
set script [string map [list %A $args %B $body %V $vars] {
foreach %V {
lappend {%A} [subst {%B}]
}
return [set {%A}]
}]
puts stderr $script
return [uplevel $script]
}
puts [Foreach i {a b c d} {- $i
}]
== Subst-switch ==
proc Switch {args} {
set switch {}
foreach {key body} [lindex $args end] {
if {$body eq "-"} {
lappend switch $key -
} else {
lappend switch $key [list subst $body]
}
}
return [uplevel [list switch {*}[lrange $args 0 end-1] $switch]]
}
foreach x {moop fnargle zip ni} {
puts [Switch $x {
moop {'$x' is moop}
fnargle -
zip {'$x' is one of fnargle or zip}
default {who cares what '$x' is}
}]
}
----
''[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.
| |
|