On 20/04/2008, at 7:18 PM, Xavier Bourguignon wrote:
So Bob, what is the difference between setting the variable within the
adp and setting it in a procedure? Surely the variable set in the
procedure should be available as the procedure is called within the
adp.
It's all about scope! A variable declared inside a procedure is only
available to that procedure, unless you alter the scope using global,
upvar or uplevel.
More information on scope here: http://wiki.tcl.tk/11921
In general, creating variables inside a procedure and then using
global or upvar/uplevel to make them available once the procedure
returns is pretty bad form.
I would recommend you use the return value of the proc for the value
you want in your ADP, like:
<%
proc foo {} {
return "bar"
}
set value [foo]
%>
<%= $value %>
You should also consider having procedure creating parts of your HTML,
which can be made easy using subst:
<%
proc createBox {title contents} {
return [subst {
<div class="boxTitle">$title</div>
<div class="boxContents">$contents</div>
}]
}
%>
<%= [createBox "It's all about scope" "blah blah blah" ] %>
The use of subst here makes it possible to not escape the " character
and still have the variables included.
While I do not use a templating system, virtually all my HTML is
created using procedures like the one above that I put in Tcl libraries.
And all of the data from the database I get using other procedures
that only select from the database and put it in a structure I can
pass into one of the HTML procedures. My ADP pages usually have not
much more than 10 to 20 lines in them; it makes everything easy to
maintain and bug-free and re-using html and logic throughout the site
is very easy.
Hope that gives you some inspiration!
Bas.
--
AOLserver - http://www.aolserver.com/
To Remove yourself from this list, simply send an email to <[EMAIL PROTECTED]>
with the
body of "SIGNOFF AOLSERVER" in the email message. You can leave the Subject:
field of your email blank.