Forum OpenACS Q&A: Response to ad_host before page is loaded...

Collapse
Posted by Jonathan Ellis on
like I said, "If you really really want to have variables pre-declared for you the only way I know of is to have a registered proc that computes your variables and then sources the requested script." look up the docs on ns_register_proc... you could probably register something like this:
proc serve_with_ad_host { url } {
    # url will have the form /foo/bar.tcl
    set file [string range $url 1 end] ;# trim the /
    set host [ad_host] ;# precompute this
    source [file join [ns_info pageroot] $file]
}
(when you source a script it's exactly like inserting the script's code at your current execution point.)

doing this you can either make host global or use upvar/uplevel. global is the cleanest, followed by upvar, but both require you to explicitly call them which you seem reluctant to do. You could make a proc such as

proc global_host {} {
    global host
    return $host
}
but that's starting to get silly to avoid the overhead of something as simple as regexp or ns_set lookup.