Forum OpenACS Q&A: Tcl API problems

Collapse
Posted by Boris Kruvshenko on
I´ve create an adp page (hello.adp) with tcl embebbed that is:

<html>
<head>API Tcl Example</head>
<body>
<h1>API Tcl Example</h1>
<p>
<%
ns_puts "Hello"
set ua [ns_set get [ns_conn headers] "user-agents"]
ns_puts "$ua"
if [string match "*MSIE*" $ua] {
  ns_puts "This is MS Internet Explorer browser."
} elseif [string match "*Mozilla*" $ua] {
  ns_puts "This is Mozilla browser."
} else {
  ns_puts "Couldn't determine your browser."
}
%>
</body>
</html>

I´ve move it to the calendar folder so I can browse it writing http://mydomain:7000/calendar/hello but when I try I get next message:

Server Error
There was a server error processing your request. We apologize.

Here is a detailed dump of what took place at the time of the error, which may assist a programmer in tracking down the problem:

This function cannot be used outside of an ADP
    while executing
"ns_puts "Hello""
    ("uplevel" body line 10)
    invoked from within
"uplevel {
          set __adp_output ""
append __adp_output "<html>
<head>API Tcl Example</head>
<body>
<h1>API Tcl Example</h1>
<p>
"

ns_puts "Hello"
..."
    (procedure "template::code::adp::/web/service0/packages/calendar/www/hol..." line 2)
    invoked from within
"template::code::${template_extension}::$__adp_stub"
    (procedure "template::adp_parse" line 68)
    invoked from within
"template::adp_parse [file root [ad_conn file]] {}"
    (procedure "adp_parse_ad_conn_file" line 5)
    invoked from within
"$handler"
    ("uplevel" body line 2)
    invoked from within
"uplevel $code"
    invoked from within
"ad_try {
                $handler
            } ad_script_abort val {
                # do nothing
            }"
    invoked from within
"rp_serve_concrete_file [ad_conn file]"
    (procedure "rp_serve_abstract_file" line 60)
    invoked from within
"rp_serve_abstract_file "$root/$path""
    ("uplevel" body line 2)
    invoked from within
"uplevel $code"
    invoked from within
"ad_try {
            rp_serve_abstract_file "$root/$path"
            set tcl_url2file([ad_conn url]) [ad_conn file]
            set tcl_url2path_info..."

I don´t know if it is possible to call tcl api functions from oacs... so if you have any idea...

Thx a lot.

Collapse
2: Re: Tcl API problems (response to 1)
Posted by C. R. Oldham on

Boris,

There are some ns_ functions that you cannot call from .adp files regardless of OpenACS. ns_adp_puts is one of them. What you have written smells like you have some PHP experience. Try this for a paradigm shift:

<html>
<head>API Tcl Example</head>
<body>
<h1>API Tcl Example</h1>
<p>
Hello
<%
 set ua [ns_set get [ns_conn headers] "user-agents"]
%>
<%= $ua %>
<%
 if [string match "*MSIE*" $ua] {
  set output "This is MS Internet Explorer browser."
 } elseif [string match "*Mozilla*" $ua] {
  set output "This is Mozilla browser."
 } else {
  set output "Couldn't determine your browser."
 }
%>
<%= $output %>
</body>
</html>

However that being said, the "right" way to do this in OpenACS is to create a hello.tcl file that contains all your set statements, and a hello.adp file that contains the presentation. It might look like this:

hello.tcl

set ua [ns_set get [ns_conn headers] "user-agents"]
 if [string match "*MSIE*" $ua] {
  set output "This is MS Internet Explorer browser."
 } elseif [string match "*Mozilla*" $ua] {
  set output "This is Mozilla browser."
 } else {
  set output "Couldn't determine your browser."
}
hello.adp

<html>
<head>API Tcl Example</head>
<body>
<h1>API Tcl Example</h1>
<p>
Hello
@ua@
@output@
</body>
</html>

Note that then the URL for the page would be http://yoursite/calendar/hello (note lack of .adp or .tcl extension).

Collapse
3: Re: Tcl API problems (response to 2)
Posted by Boris Kruvshenko on
Thanks C.R.
So if for example I want to create a folder, instead of use ns_mkdir... what should I do in oacs ??
Collapse
4: Re: Tcl API problems (response to 1)
Posted by C. R. Oldham on
Functions like ns_mkdir, etc, will work fine, you just have to watch out for ns_ or standard tcl functions that want to write something to stdout or elsewhere.

Have you done the problem sets?  They are a little outdated, but are still fairly good.  Also, I highly recommend looking at the documentation for the acs-templating package.

Collapse
5: Re: Tcl API problems (response to 1)
Posted by Jade Rubick on