Difference between revisions of "Ns adp registerproc"

From AOLserver Wiki
Jump to navigation Jump to search
(imported from WiKit id 869)
 
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
Man page: http://aolserver.com/docs/tcl/ns_register_proc.html
 
 
----
 
 
 
'''NAME'''
 
'''NAME'''
  
:       ns_register_proc - Register a procedure for a method/URL combination
+
: ns_adp_registerproc - Register an XML-like tag for use within an ADP.
  
 
'''SYNOPSIS'''
 
'''SYNOPSIS'''
  
:       '''ns_register_proc''' ''?-noinherit?'' method URL myproc ''?args?''
+
: '''ns_adp_registerproc''' tag ''?endtag?'' proc
  
 
'''DESCRIPTION'''
 
'''DESCRIPTION'''
  
:             '''ns_register_proc''' registers the procname to handle
+
: '''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.
:              the specified method/URL  combination. When  the
 
:              server  gets  a matching request, it calls procname
 
:              with the connection id and any arguments  specified
 
:              here.
 
  
:             If  ''-noinherit'' is specified, the requested URL must
+
: 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.
:              match the specified URL exactly. For  example, if
 
:              the   URL  specified  with  '''ns_register_proc'''  is
 
:              /foo/bar, procname will not be  called  unless  the
 
:              requested URL is exactly /foo/bar.
 
  
:             If  ''-noinherit'' is not specified, the requested URL
+
: There are two ways to use '''ns_adp_registerproc''', with and without the ''endtag'' parameter:
:              can match the specified URL or any  URL  below  it.
 
:              For  example,  if  the URL specified with '''ns_register_proc'''  
 
:              is /foo/bar, procname will be  called  for
 
:             /foo/bar,  /foo/bar/hmm,  and  any  other URL below
 
:              /foo/bar, provided there  is  not  already  another
 
:              procedure  registered  for that exact URL or for an
 
:              URL with a closer match.
 
  
:              Note that you must use a glob-style matching  character 
+
* If the endtag parameter is specified, the procedure you specify with proc must be of the form:
:              if  you want inheritance for file names. For
 
:              example,  if  you  want  /foo/bar  to    match
 
:              /foo/bar.html,   you   must use:
 
  
                      ns_register_proc /foo/bar*
+
    proc myadpproc { args }
  
:             You can  register  two  procedures  for any  given
+
: 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.
:              method/URL  combination by calling '''ns_register_proc'''
 
:              once with the ''-noinherit'' flag set and once  without
 
:              it. Only  one of the procedures will be called for
 
:              any given request, depending on whether the URL was
 
:              an exact match or not. For example:
 
  
                    ns_register_proc -noinherit GET /foo/bar Aproc
+
: 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.
                    ns_register_proc GET /foo/bar Bproc
 
                    ns_register_proc GET /foo/bar/hmm Cproc
 
  
:              Aproc  will  be  called  when  the requested URL is
+
* 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:
:              exactly /foo/bar. Bproc will  be  called  when  the
 
:              requested  URL is below /foo/bar, provided there is
 
:              not already  another  procedure registered  to  be
 
:              called  for  that  exact  URL  or for an URL with a
 
:              closer match. Cproc (not Bproc) will be called when
 
:              the   requested  URL  is   equal  to  or  below
 
:              /foo/bar/hmm. Syntax for the registered  procedure
 
:              The conn (connection) argument is optional for procedures
 
:              registered by ns_register_proc if the procedure  has 
 
:              0 or 1 arguments (not including conn).
 
:              The following examples show the variations that can
 
:              be used in this case:
 
  
                    ns_register_proc GET /noargs noargs
+
    proc myadpproc { args }
                    ns_register_proc GET /context context fnord
 
                    ns_register_proc GET /conncontext conncontext greeblev
 
 
                    proc noargs { } {
 
                        ns_returnnotice 200 "noargs"
 
                    } ;# noargs
 
 
                    proc context { context } {
 
                        ns_returnnotice 200 "context is $context"
 
                    } ;# context
 
 
                    proc conncontext { conn context } {
 
                        ns_returnnotice 200 "conncontext is $context"
 
                    } ;# conncontext
 
  
:              The conn (connection) argument is required for procedures
 
:              registered by '''ns_register_proc''' if the  procedure 
 
:              has  2  or  more  arguments  (not including
 
:              conn). The conn argument will be  filled  automatically 
 
:              with  the  connection information. The first
 
:              argument following conn will always take the  value
 
:              supplied  by  '''ns_register_proc''', if there is one, or
 
:              an empty value. All other arguments must  supply  a
 
:              default  value.  The  following  examples  show the
 
:              variations that can be used in this case:
 
  
                    ns_register_proc GET /twoargs twoargs fnord
+
'''EXAMPLE'''
                    ns_register_proc GET /threeargs threeargs fnord fjord
 
 
                    ns_register_proc -noinherit GET /foo/bar Aproc
 
                    ns_register_proc GET /foo/bar Bproc
 
                    ns_register_proc GET /foo/bar/hmm Cproc
 
  
:             Aproc  will  be  called  when  the requested URL is
+
: Suppose you want to register a tag that retrieves some url using the Tcl http package. You could register the tag as follows:
:              exactly /foo/bar. Bproc will  be  called  when  the
 
:              requested  URL is below /foo/bar, provided there is
 
:              not already  another  procedure  registered  to be
 
:              called  for  that exact  URL  or for an URL with a
 
:              closer match. Cproc (not Bproc) will be called when
 
:              the   requested  URL  is  equal  to  or  below
 
:              /foo/bar/hmm. Syntax for the registered  procedure
 
:              The conn (connection) argument is optional for procedures
 
:              registered by '''ns_register_proc''' if the  procedure 
 
:              has  0 or 1 arguments (not including conn).
 
:              The following examples show the variations that can
 
:              be used in this case:
 
  
                    ns_register_proc GET /noargs noargs
+
    ns_adp_registerproc "geturl" "/geturl" geturlproc
                    ns_register_proc GET /context context fnord
 
                    ns_register_proc GET /conncontext conncontext greeble
 
 
 
                    proc noargs { } {
 
                        ns_returnnotice 200 "noargs"
 
                    } ;# noargs
 
 
                    proc context { context } {
 
                        ns_returnnotice 200 "context is $context"
 
                    } ;# context
 
 
                    proc conncontext { conn context } {
 
                        ns_returnnotice 200 "conncontext is $context"
 
                    } ;# conncontext
 
  
:             The conn (connection) argument is required for procedures
+
    # Note: We are registering a Tcl proc.
:             registered by ns_register_proc if the  procedure  has  2 
+
    proc geturlproc {args} {
:             or  more  arguments  (not including
+
        set url [lindex $args end]
:             conn). The conn argument will be  filled  automatically 
+
        set args [lrange $args 0 end-1]
:             with  the  connection information. The first
+
        set token [eval ::http::geturl [list $url] $args]
:             argument following conn will always take the  value
+
        set result [::http::data $token]
:             supplied  by  ns_register_proc, if there is one, or
+
        ::http::cleanup $token
:             an empty value. All other arguments must  supply  a
+
        return $result
:             default  value.  The  following  examples  show the
+
    }
:             variations that can be used in this case:
 
  
                    ns_register_proc GET /twoargs twoargs fnord
+
: Then, in an ADP, you use these tags:
                    ns_register_proc GET /threeargs threeargs fnord fjord
 
 
                    proc twoargs { conn context { greeble bork } } {
 
                        # Do stuff...
 
                    }
 
 
                    proc threeargs { conn context {greeble bork } { hoover quark } {
 
                        # Do stuff...
 
                    }
 
  
:             When a GET of /twoargs is requested, the conn argument 
+
    <geturl -timeout=30 -binary=0>http://www.aolserver.com/</geturl>
:              will  be  filled  automatically,  the context
 
:              argument will be assigned "fnord" and  the  greeble
 
:              argument will be assigned the default value "bork".
 
  
:             When a GET of /threeargs  is  requested,  the conn
+
: 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]]:
:             argument  will be filled automatically, the context
+
 
:              argument will be assigned "fnord" and  the  greeble
+
    ns_adp_registerproc adpControl adpControlProc
:             argument  will  be assigned "fjord", and the hoover
+
 
:             argument  will  be assigned  the  default  value
+
    # Note: We are registering a Tcl proc.
:              "quark".
+
    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.
  
 
'''SEE ALSO'''
 
'''SEE ALSO'''
 +
 +
: [[ns_adp_registerscript]], [[ns_adp_parse]]
 +
 +
[[Category:Documentation]] [[Category:Core Tcl API]]

Latest revision as of 08:19, 14 October 2009

NAME

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

SYNOPSIS

ns_adp_registerproc tag ?endtag? proc

DESCRIPTION

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 }


EXAMPLE

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.

SEE ALSO

ns_adp_registerscript, ns_adp_parse