Ns adp registerproc

From AOLserver Wiki
Revision as of 08:19, 14 October 2009 by Akhassin (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search


ns_adp_registerproc - Register an XML-like tag for use within an ADP.


ns_adp_registerproc tag ?endtag? proc


ns_adp_registerproc registers a procedure to be called when the specified beginning and ending tags are used in an ADP. The tag is the beginning tag to look for, and the endtag is the ending tag to look for. The proc is the procedure that will be called when AOLserver encounters those tags when processing an ADP.
Try not to confuse ns_adp_registerproc with ns_adp_registerscript. Both register a Tcl script/proc that is run by the tag. However, this one receives individual arguments for the tag attributes and the other receives them in a single ns_set.
There are two ways to use ns_adp_registerproc, with and without the endtag parameter:
  • If the endtag parameter is specified, the procedure you specify with proc must be of the form:
   proc myadpproc { args }
In args, the proc will receive a variable number of arguments for the attributes that were specified with the beginning tag and the last argument will be the string of characters between the beginning tag and the ending tag. The return value of the procedure will be sent to the browser in place of the string of text that was specified between the beginning and ending tags.
In AOLserver 4.0 and older, the string is not parsed, which means that you could not include ADP tags in the string unless you execute ns_adp_parse on the string inside the procedure that processes the registered ADP tag. In AOLserver 4.5, <% %> ADP tags in the string do get parsed.
  • If endtag is not specified, then no closing tag is required. The procedure (proc) will be called every time the specified command is encountered. The procedure will receive variable number of arguments for the attributes to the tag:
   proc myadpproc { args }


Suppose you want to register a tag that retrieves some url using the Tcl http package. You could register the tag as follows:
   ns_adp_registerproc "geturl" "/geturl" geturlproc
   # Note: We are registering a Tcl proc.
   proc geturlproc {args} {
       set url [lindex $args end]
       set args [lrange $args 0 end-1]
       set token [eval ::http::geturl [list $url] $args]
       set result [::http::data $token]
       ::http::cleanup $token
       return $result
Then, in an ADP, you use these tags:
   <geturl -timeout=30 -binary=0>http://www.aolserver.com/</geturl>
In another example we show how to use a registered tag without an endtag. This tag provides a declarative way of controlling adp parsing parameters and gives safe-parsed adp pages the ability to call ns_adp_ctl:
   ns_adp_registerproc adpControl adpControlProc
   # Note: We are registering a Tcl proc.
   proc adpControlProc { args } {
       foreach {option value} $args {
           ns_adp_ctl $option $value
In an ADP, you use this tag:
   <adpControl nocache=1 gzip=0>
This can be extended to provide XML-like template pages for content.


ns_adp_registerscript, ns_adp_parse