<:args [argname] [argname1=expr] [argname2=expr...]:>
This tag makes short and quick work of handling arguments submitted by the user of your website through a GET or POST request. It allows you to copy CGI arguments into local variables of the same name, convert the CGI arguments (which are always strings) into Python data types, and assign default values to those variables if the conversion fails or if the caller failed to pass them in.
Each argument of this tag specifies a name, and optionally a Python expression value. The name of each argument indicates the name of the user argument and also the name of the local variable to hold the value. If you just specify this name, it will copy the user argument of that name into a local variable of that name. If the user passed no argument by that name, it assigns the None object to the variable.
If you do specify a Python expression as a value, it will examine the expression, and take the following actions:
<:args this=`int`:>
In the above STML, the user argument named this will be converted into an integer with the built-in Python function int. Then the result will be assigned to a local variable this. If anything goes wrong, or if the user passed no this argument, the local variable this will be None.
<:args this=`(int, 5)`:>
In this example, SkunkWeb looks for the user argument named this. If it's there, SkunkWeb calls the function int with the value, and then assigns the rsult to the local variable this. If the user passed no arugment named this, or if the call to int fails in some way, SkunkWeb assigns the integer 5 to this.
This tag only needs to be used once, near the beginning of the STML document which serves the Web request. However, you may use it as many times as you like. Each time this tag executes, it looks in the SkunkWeb dictionary object CONNECTION.args, extracts values from it, and performs the necessary conversion and variable assignments. You cannot use this tag in an STML component, however; STML components do not have access to any part of the CONNECTION object (unless of course, the CONNECTION object was passed to it explicitly).
Also note that you are not limited to the built-in Python functions int, float, long, etc. for your conversion functions. Any function, or any callable object for that matter, can be used. Here's a large example of an <:args:> tag that does a lot of work:
<:import Date:> <:args foo bar=`0` baz=`(int, 5)` dib=`(float, 0.0)` gad=`(Date.LocalDate, Date.LocalDate())` :>
As of SkunkWeb 3.2, the CONNECTION object has a method, extract_args, which can be used for the same purpose whenever the CONNECTION object is present (e.g., in python top-level documents). CONNECTION.extract_args returns a dictionary of extracted values; to achieve the same result as above in pure Python, you would do the following:
import Date d=CONNECTION.extract_args(foo, bar=0, baz=(int, 5), dib=(float, 0.0), gad=(Date.LocalDate, Date.LocalDate())) locals().update(d)