Forum OpenACS Q&A: return from tcl without parsing adp?

Collapse
Posted by tammy m on
Hi

I want to return from a tcl file without parsing it's adp file in 1 case. I tried calling ad_abort_script but this returned an empty page. Basically, I'm receiving a validated FORM and I want to forward the user to a new page that displays all my FORM input from the user all pretty-like. When I use template::forward new-page I don't have access to all my FORM vars in the new adp page.

So I tried this approach instead.

if { [template::form::is_valid $form_name] } {

   # Sets local variables for FORM elements/values.
   template::form::get_values $form_name

   # From https://openacs.org/doc/acs-templating/appendices/memory.html :
   # compile the template.  The templating system takes the
   # result of this step and wraps it in a proc so that it is
   # bytecode-cached in the interpreter.  You may wish to implement
   # some sort of simple caching here as well.
   set code [::template::adp_compile -file [string map {".adp" "-display.adp"} [ad_conn file] ] ]

   # evaluate the template code.  Note that you pass a reference
   # to the variable containing the code, not the value itself.  The code
   # is evaluated in the calling stack frame (by uplevel) so that
   # the above data sources are directly accessible.
   set output [::template::adp_eval code]

   # now use the output however you wish

   # Already done for you.
   # lappend body $__adp_output

   ad_script_abort

#    template::forward display-ad
#    template::forward index.html
}    

Is this just a whacked way of doing this?! Can anyone show me the better way to get my FORM vars to a new adp template for display?

TIA:)

Collapse
Posted by Tilmann Singer on
Check the api-docs for rp_form_put and rp_internal_redirect, I think that should do the trick.
Collapse
Posted by tammy m on
Erghhhh

Not to appear too boneheaded or lazy but now I can't for the life of me figure out how to rp_internal_redirect to a new page, and feed that page with certain query variables.. I'm sure it has something to do with ad_page_contract but can't figure out how to specify query variables to the adp I'm redirecting to:(

Help! + thanks again.

Collapse
Posted by Dave Bauer on
Tammy,

You want ad_return_redirect my_page?[export_vars {var1 var2 var3}]

THere are examples of this on most of the tcl pages that process forms.

Collapse
Posted by Dave Bauer on
Oops. I totally forgot. Make sure you call ad_script_abort after ad_returnredirect unless you have some tcl code that must be processed after the user is redirected. That is rarely the case, so most of the time you will call ad_script_abort.
Collapse
Posted by Randy O'Meara on
I think Til's suggestion is the elegant solution if it works. I use the same technique in an index.vuh script which would correspond to your originating tcl page.

index.vuh

# Put common values into form vars
set vuh_title "some value"
set vuh_user_id "some other value"
rp_form_put vuh_title $vuh_title
rp_form_put vuh_user_id $vuh_user_id
...
# Be *really* careful about recursion here. There is no
# protection as far as I can tell. Symptoms are sudden
# unexplainable aolserver restarts.
rp_internal_redirect "anotherpage.tcl"

anotherpage.tcl

ad_page_contract {
...
} -query {
    {vuh_user_id}
    {vuh_title}
}...
Collapse
Posted by Stan Kaufman on
Dave, the problem with ad_returnredirect is that whenever a user comes to the site with IE 5+, they get the ugly util_ReturnMetaRefresh hack. You can write a new version of it to give the user a templated page that looks like the rest of the site, but still. It's ugly. And nearly all users of OACS sites are using IE these days, eh?

I see that util_ReturnMetaRefresh is deprecated in HEAD. Is there a better solution to the problem of IE not handling multipart/form-data correctly? If so, what is it?

Collapse
Posted by Dave Bauer on
You can use rp_internal_redirect if you want it to appear that the URL has not changed.

Stan,

As ad_returnredirect is used on almost every form page, what other option is there?

Collapse
Posted by Stan Kaufman on
Dave, that's exactly my question. What's the alternative? What is supposed to happen when util_ReturnMetaRefresh goes away?
Collapse
Posted by Tilmann Singer on
Dave's right, I misread your question and thought you required that the url does not change. So there are two options: 1) simple, when the url can change use ad_returnredirect and ad_script_abort like in hundreds of places all over the toolkit 2) if the url should not change, use rp_internal_redirect according to Randy's example.

One correction to Randy's example: you should use abstract urls for the target, e.g. say

rp_internal_redirect "anotherpage"

instead of

rp_internal_redirect "anotherpage.tcl"

Collapse
Posted by tammy m on
Hi

Thanks again all for the help and especially code samples:)

What I ended up doing was basically like this simple sample. I think this works best for me because I really didn't wanna have to pass all my variables as query parameters to the adp page... there is going to be about 60 of them POSTed to the original FORM page afterall!

Page that is requested by user's browser

ad_page_contract {

    ad_page_contract basic usage:
        1st arg: comments on current page params + usage
        2nd arg: parameters current tcl page is expecting to receive
        3rd arg: -properties flags "properties" or parameters passed to
                adp page associated with this tcl page (ad_return_template)
        
    @creation-date 06/13/2003
    @cvs-id $Id: $
} {            
}          
   rp_form_put first_name Scooby
   rp_form_put last_name Doo
   rp_internal_redirect my-display-page
   ad_abort_script

my-display-page.tcl

ad_page_contract {

    ad_page_contract basic usage:
        1st arg: comments on current page params + usage
        2nd arg: parameters current tcl page is expecting to receive
        3rd arg: -properties flags "properties" or parameters passed to
                adp page associated with this tcl page (ad_return_template)
        -query flags args POSTed to adp via ad_return_redirect or  rp_internal_redirect.
               See return from tcl without parsing adp?
               
               Be *really* careful about recursion using rp_internal_redirect.
               There is no protection as far as I can tell. Symptoms are sudden
               unexplainable aolserver restarts.

    @creation-date 06/13/2003
    @cvs-id $Id: $
} {
  {first_name:notnull "NO firstname:("}
  {last_name:notnull "NO lastname:("}
} 

my-display-page.adp

              
 

FORM input received

@first_name@ @last_name@