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

Collapse
Posted by David Kuczek on
I am using ad_host on 3.2.5 and would like to have this variable
available before a page is loaded, so that I can get it with "upvar
#1 ad_host ad_host"...

How can I do this?

Collapse
Posted by Jonathan Ellis on
It's almost certainly cleaner & better to make it a proc instead.  Can you be more specific?
Collapse
Posted by David Kuczek on
Hello Jonathan,

I want to define the variable ad_host via the 4.5 "ad_host proc" (I just moved it to 3.2.5) for every page that is being loaded on my server, so that I don't have to define it in i.e. ad_header AND ad_footer, but only once and then get it with "upvar #1 ad_host ad_host".

I also have a pseudo subsite system that defines different ad_headers or different colored bars (ad_main_bar) for each subsite i.e. green for foo1.mysytem.com and black for foo2.mysystem.com...

Or doesn't it matter performance wise that I call ad_host around 10 times within a page (ad_footer, ad_header, 8x ad_main_bar)???

Collapse
Posted by Jonathan Ellis on
You'd have to do some timing to see if this is going to adversely effect your performance, but I'd guess that 10 or so regexp calls would be less expensive than a single db query.

My first step if it turned out to need optimization would be to have ad_host store its regexp results in a ns_set.  You'd want to time that as well.

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.

Collapse
Posted by David Kuczek on
Is there a possibility to have this variable "ad_host" always available and retrievable via upvar no matter what page a user requests from the server? Like a procedure that sets this variable before a page is called... Beg my pardon if you already gave me an answer and I just didn't understand it correctly...

I have to admit that I don't really get the functionality with variables (global, upvar, uplevel etc.)...

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.
Collapse
Posted by Don Baccus on
You can mimic what is done for ad_conn in OpenACS 4.5 if you really want to avoid the minimal overhead associated with calling ad_host.  Use a filter to set your global value before the script's executed, then either reference the global directly in your script or use a two-liner proc to return the value as suggested in the previous post.