Alternate Python String Format Syntax 4
The special name-space variable, _ 6
Using Document Templates from Python 12
Creating document templates 14
Using document templates with Bobo 15
Custom, special, C, and empty formats 16
Conditional insertion, the if and unless tags 19
Iterative insertion, the in tag 20
The else tag as an intermediate tag in the in tag 21
Variables defined by the in tag 22
Showing row number and row data in previous and next batch hyper links. 27
Showing information about multiple batches 28
Displaying objects with the with tag 30
Evaluating names or expressions without generating any text using the call tag 31
Reporting errors with the raise tag 32
TABLE 1. Expression examples 5
TABLE 2. Available attributes in the special variable, _ 6
TABLE 3. Attributes defined by the math module. 7
TABLE 4. Attributes defined by the string module 8
TABLE 5. Attributes defined by the whrandom module 9
TABLE 6. Simplest-case steps for looking up names 11
TABLE 7. Bobo-defined Web-request variables, 11
TABLE 8. Attributes of the REQUEST variable 12
TABLE 9. Attributes of the RESPONSE variable. 12
TABLE 10. CGI-defined Web-request variables 13
TABLE 11. Document template classes 13
TABLE 12. Standard document template creation arguments. 14
TABLE 13. Standard arguments for calling document templates. 14
TABLE 14. var tag attributes 16
TABLE 15. Special formats that may be used with the var tag fmt attribute 17
TABLE 16. C-style specifiers for the fmt attribute 17
TABLE 17. in tag attributes 20
TABLE 18. Item variables defined by the in tag 22
TABLE 19. Summary statistic variables defined by the in tag 23
TABLE 20. Batch-processing variables 24
TABLE 21. Attributes of batch objects used when iterating over next-batches and previous-batches variables. 24
TABLE 22. Query strings, and previous batch URL and next batch URL for the batches shown in figure 6. 27
Figure 1. DTML source to create an employee phone listing 20
Figure 2. DTML source to create an employee phone listing, while properly handling the case of no employees, using an in tag with an if tag. 21
Figure 3. DTML source to create an employee phone listing, while properly handling the case of no employees, using an else tag in an in tag. 22
Figure 4. Table of 36 words 25
Figure 5. DTML source to browse 36 words, 5 words at a time 26
Figure 6. The output of the DTML source in figure 5 as displayed in a Web browser for several batches. 26
Figure 7. Using batch-processing variables to show previous and next batch row number using Roman and Arabic numerals. 27
Figure 8. Using batch-processing variables to show previous-batch row number using letters and next-batch starting and ending words. 28
Figure 9. Use of DTML to provide links to all previous and next batches. 29
The Document Template Markup Language (DTML) is a facility for generating textual information using a template document and application information. It is used in Bobo primarily to generate Hypertext Markup Language (HTML) files, but it can also be used to create other types of textual information.
The DTML facility is used to convert from document template source text to rendered text. Document template source text consists of ordinary text interspersed with DTML "markup" tags.
The next sections describe the syntax of DTML tags and provide a number of simple examples of document template source texts. Later sections provide detailed information on each of the DocumentTemplate tags provided with Bobo.
Two syntaxes are currently supported 1 by document templates. This section describes a syntax that is based on HTML server-side includes. The next section describes a syntax that is based on standard Python string formats. The syntax used depends on the document template class used (or subclassed). The server-side-include syntax described here is used by the classes DocumentTemplate.HTML and DocumentTemplate.HTMLFile.
The syntax used by DTML to indicate text substitution is based on the use of DTML tags. A DTML tag is of the form:
<!--#tag-name attribute1="value1" attribute2="value2" ... -->
The tag name controls the tag type. The tag typically has one or more attributes that indicate where the tag gets it's data and other information that controls how data are inserted. Sometimes attribute values can be omitted and quotes around attribute values can be omitted if the values don't contain spaces, tabs, new-line characters, equal signs or double quotes.
The most common tag is the var tag. The var tag is used to substitute variable data into text. Suppose we wanted to create a greeting with a variable input_name . We might use a document template like the following:
Hello <!--#var name="input_name" capitalize-->!
This example uses two attributes, name and capitalize . The name attribute is used to specify a variable name. Typically, variable names refer to data in World-Wide-Web (Web) requests, or properties of objects. Because the name attribute is so commonly used, a short-hand version of the name attribute is allowed in which the attribute name is omitted, as in:
Hello <!--#var input_name capitalize-->!
When using the short-hand form of the name attribute, the value must be unquoted and the attribute must be the first attribute in the tag.
A similar short-hand notation is used for the expr attribute. The expr attribute is used to provide computational expressions as in:
This may be shortened by eliminating the attribute name, as in:
When using the short-hand form of the expr attribute, the value must be quoted and the attribute must be the first attribute in the tag.
The capitalize attribute illustrates the use of attributes that need not have values 2 . The capitalize attribute indicates that the inserted text should be capitalized. For example, suppose the document template source above was evaluated with an input name of " world ", then the text output would be:
The var tag is said to be an empty tag , because it doesn't contain any other tags. Some tags are said to be non-empty because they bracket text that may contain other DTML tags. Non-empty tags have matching end tags . The name of the end tag is the name of the start tag except that it has a " / " or an " end " prefix. A commonly used non-empty tag is the if tag. An example of an if tag is:
<!--#if input_name-->
Hello <!--#var input_name-->.
<!--#/if-->
In this example, if the variable, input_name , has not been provided, or is an empty string, then the greeting is omitted. End tags do not have attributes.
A non-empty tag may also have intermediate tags. These intermediate tags serve to break the non-empty tag into two or more sections. For example the if tag can use an intermediate else tag, as in:
<!--#if input_name-->
Hello <!--#var input_name-->.
<!--#else-->
Have we been introduced?
<!--#endif-->
Note that in this case, the alternate form of the end tag is used.
Sometimes, intermediate tags may have attributes, as in:
<!--#if input_name-->
Hello <!--#var input_name-->.
<!--#elif nick_name-->
Hi <!--#var nick_name-->.
<!--#else-->
Have we been introduced?
<!--#/if-->
In the example above, there is one non-empty tag, if , that uses two intermediate tags, elif and else , and an end tag, /if .
Any number of line endings, tabs, and spaces may be used between the pound character (#), the tag name, attributes, and the end of a tag. For example, the following are all valid tags:
<!--#
var x--> <!--#var y -->
<!--#var some_really_long_name
--><!--#var and_another_rather_long_one
-->
This section describes the extended Python string format syntax. The extended Python string format syntax is used by the classes DocumentTemplate.String and DocumentTemplate.File. This format is based on the insertion-by-name format strings of python with additional format characters, '[' and ']' to indicate block boundaries. In addition, attributes may be used within formats to control how insertion is done. For example:
causes the contents of variable 'date' to be inserted using custom format 'DayOfWeek' and with all lower case letters converted to upper case.
Document template strings use an extended form of python string formatting. To insert a named value, simply include text of the form:
where 'name' is the name of the value and 'x' is a format specification, such as '12.2d'. To introduce a block such as an 'if' or an 'in' or a block continuation, such as an 'else', use '[' as the format specification. To terminate a block, use ']' as the format specification, as in:
%(if input_name)[
Hello %(var input_name size=16 etc="...")s.
%(elif nick_name)[
Hi %(var nick_name capitalize)s.
%(else)[
Have we been introduced?
%(/if)]
This section described two attributes that are used by most DTML tags. Both of these attributes are used to identify or compute data to be used by the tag.
The name attribute is used to obtain data by name. The data is looked up using the rules described in the section See Name lookup below. The name attribute is special because the attribute name may be omitted, as described in the section See DTML Tag Syntax above.
When a value is looked up with the name attribute, it is also called, if possible. If the value is a document template, it is rendered before being given to the tag that uses the name attribute.
If the value is a function 3 that can be called with no arguments, then the result of calling the function will be given to the tag using the name attribute.
When the name attribute is used in an if , elif , unless , in or with tag, the value associated with the name is cached so that references to the name in enclosed text are faster than the initial reference. This is especially important when the name refers to a function that is computationally expensive. For example, in:
<!--#if reallyExpensiveFunction-->
<!--#var reallyExpensiveFunction-->
<!--#/if-->
The var tag uses the cached value for reallyExpensiveFunction . Note that tags like the in and with tag that introduce new variables may introduce a new value for the given name that override the cached value.
The expr attribute allows more or less arbitrarily complex expressions to be evaluated.
The expression syntax is that of Python. Table See Expression examples provides some simple expression examples that illustrate the expression syntax and capabilities.
Invoke method 4 meth of obj with arguments a and b |
|
The expression used in an expr attribute is enclosed in double quotes and double quotes are not allowed in the expression.
The variable names used in an expr expression are looked up according to the rules described in See Name lookup below. Looked up values are not "called" as they are when the name attribute is used. In the sample expressions in table See Expression examples , the names x , func , a , b , obj , age , and status are variable names, while the names meth and title are object attribute names.
Variable names must begin with a letter and contain only letters, digits, and underscore characters. To access a variable with a name that doesn't follow these rules, it is necessary to use the special variable, _, described in See The special name-space variable, _ , below.
A special variables, _ , is defined for use in expr expressions. The _ variable provides access to the DTML "name space", which is an object used to lookup variables when rendering DTML. The _ variable can be used to lookup names directly:
<!--#if "_['sequence-length'] > 20"-->
The _ variable is especially useful for accessing variables with names that contain special characters, like dashes, as in the case above.
The _ variable has a method, has_key that can be used to check whether a variable can be looked up:
<!--#if "_.has_key('sequence-length')"-->
Note that when a value is looked up with the _ variable, and the value is callable, then it is called automatically, as is done with the name attribute. Sometimes, you don't want to have the value called automatically. To avoid calling the value, look it up with the getitem method of the _ variable. The getitem method accepts two arguments, the name to be looked up and a flag indicating whether the value is to be called:
<!--#var "_.getitem(name, 0)"-->
The variable _ provides access to a number of useful general purpose objects and functions as attributes. The available attributes are shown in table See Available attributes in the special variable, _ .
Return a string of one character whose ASCII code is the integer I , e.g., chr(97) returns the string 'a' . This is the inverse of ord() . The argument must be in the range 0-255, inclusive. |
|
Take two numbers as arguments and return a pair of integers consisting of their integer quotient and remainder. With mixed operand types, the rules for binary arithmetic operators apply. For integers, the result is the same as (A/B,A%B) . For floating point numbers the result is the same as (math.floor(A/B),A%B) . |
|
Convert a number to floating point. The argument may be a plain or long integer or a floating point number. |
|
Lookup a name in the namespace. If the value is callable and the flag is true, then the result of calling the value is returned, otherwise the value is returned. |
|
Return the length (the number of items) of a collection of objects. |
|
The math module (table See Attributes defined by the math module. ), which defines mathematical functions. |
|
The namespace function can be used with the in tag to assign variables for use within a section of DTML. The function accepts any number of "keyword" arguments, which are given as name=value pairs. |
|
Return the ASCII value of a string of one character. E.g., ord('a') returns the integer 97 . This is the inverse of chr() . |
|
Return X to the power Y . The arguments must have numeric types. With mixed operand types, the rules for binary arithmetic operators apply. The effective operand type is also the type of the result; if the result is not expressible in this type, the function raises an exception; e.g., pow(2,-1) or pow(2,35000) is not allowed. |
|
Return the floating point value X rounded to N digits after the decimal point. If N is omitted, it defaults to zero. The result is a floating point number. Values are rounded to the closest multiple of 10 to the power minus N; if two multiples are equally close, rounding is done away from 0 (so e.g.
round(0.5)
is
1.0
and |
|
The string module (table See Attributes defined by the string module ), which defines string functions. |
|
The whrandom module (table See Attributes defined by the whrandom module ), which defines random-number generating functions using the Wichmann-Hill pseudo-random number generator. |
Chooses a random element from the non-empty sequence seq and returns it. |
|
Initializes the random number generator from the integers X, Y and Z. |
|
Document templates provide a basic level of access control by preventing access to names beginning with an underscore 5 . Additional control may be provided by providing document templates with a 'validate' method. This would typically be done by subclassing one or more of the DocumentTemplate classes.
If provided, the 'validate' method will be called when objects are accessed as instance attributes or when they are accessed through keyed access in an expression. The 'validate' method will be called with five arguments:
If a document template was called from Bobo, then the name-space object will have an attribute, AUTHENTICATED_USER that is the user object that was found if and when Bobo authenticated a user.
When a variable name is used in a DTML tag, such as a var tag, or in an expr attribute expression, data matching the name must be looked up. Table See Simplest-case steps for looking up names shows the steps taken to look up data in the simplest case.
The mapping object and keyword arguments supplied when calling the document template are searched, with keyword arguments taking precedence over the mapping object. |
|
Attributes, including inherited and acquired attributes, of the client passed in the call to the document template are searched. |
|
Search Bobo-defined Web-request data (table See Bobo-defined Web-request variables, ). |
|
Search variables named URLn , where n is a digit. URL0 is the Request URL without a query string. URL1 is the same as URL0 except that the last name in the URL is removed. URL2 is the same as URL0 except that the last two names are removed, and so on. For example, If the request URL is http://digicool.com/A/B , then URL0 , URL1 , and URL2 are http://digicool.com/A/B , http://digicool.com/A , and http://digicool.com . URL3 is undefined. |
|
Search CGI-defined Web-request variables. See table See CGI-defined Web-request variables for a description of CGI-defined variables. |
|
Search HTTP Headers. The variable names associated with HTTP headers consists of the HTTP header name, converted to upper case, with the Prefix, HTTP_ . For example, an HTTP Referer header, if present, can be accessed using the variable name HTTP_REFERER . |
|
Search variables named BASEn , where n is a digit. BASE0 is the prefix of the request URL up to but not including the name of the module published by Bobo. BASE1 is the request URL up to and including the name of the module published by Bobo. BASE2 is the request URL up to the name following the name of the module published by Bobo, and so on. For example, assume that a module published by Bobo has the URL: http://digicool.com/Demos/Plutonia , and that a request URL is http://digicool.com/Demos/Plutonia/Marketing Then BASE0 is http://digicool.com/Demos, BASE1 is http://digicool.com/Demos/Plutonia, and BASE2 is http://digicool.com/Demos/Plutonia/Marketing . BASE3 is undefined. |
There are two situations that modify the search rules for the simplest case. If a document template is called in a DTML expr attribute expression, then additional variables may be passed in. Variables passed in take precedence over all variables described in table See Simplest-case steps for looking up names . .
An object that represents an authenticated user. When inserted into a DTML document, the value is the user name. This object currently provides no public attributes. Note that this variable may not be defined in Documents that are not protected by security information. |
|
This is the path to the object containing the user database that contained the definition for the authenticated user. |
|
A sequence of ancestors of the object that was accessed in the current request. |
|
An object that represents the current request. This object may be used in an expr expression to lookup request data, including variables described in this table, CGI-defined variables (table See CGI-defined Web-request variables ), form variables, cookies, and HTTP headers. In addition, expr expressions may use request attributes defined in table See Attributes of the REQUEST variable . |
|
An object that represents the response to the current request. This object is primarily useful in expr expressions using attributes defined in table See Attributes of the RESPONSE variable. . |
|
The URL used to invoke the request without the query string, if any. |
Some DTML tags define additional variables. Variables defined by DTML tags take precedence over variables described in table See Simplest-case steps for looking up names . If tags are nested, variables defined in nested tags take precedence over variables defined in tags that are nested in.
Names may not begin with an underscore, except in the special case of the _ variable used in an expr attribute expression.
Document templates are made available using the DocumentTemplate package 6 . The DocumentTemplate package defines four classes to be used depending on whether source is stored in Python strings or in external files and on whether the HTML server-side include syntax or the extended Python string format syntax is used. The four document template classes are shown in table See Document template classes .
Document templates are created by calling one of the classes listed in table See Document template classes . The source is passed as the first argument. An optional mapping argument may be provided that contains names to be added to the document template name-space when called and default values. An optional third argument may be provided to specify a __name__ attribute for the document template. The standard document template creation arguments are listed in table See Standard document template creation arguments. .
In addition to the standard creation arguments, additional keyword arguments may be provided to provide additional names and default values for the document template name-space. For example, in:
results=DocumentTemplate.HTMLFile('results.dtml',
{'table_name': 'search results', 'database': 'data'},
pid=os.getpid(),
time=time.time
)
A document template is created using server-side-include syntax source from an external file named 'results.dtml' and with an initial name space that included the names 'table_name', 'database', 'pid', and 'time'.
To generate text using a document template, the document template must be called. Arguments may be provided to supply an object from which to get data, a mapping object to get data from, or keyword arguments containing additional data. The standard arguments for calling document templates are shown in table See Standard arguments for calling document templates. .
An object with attributes to be included in the document template name space. |
||
A mapping objects with names and values to be added to the name space. |
Both of the standard argument may be omitted. If the mapping argument is to be provided positionally without a client, then a None must be passed as the client value, as in:
return results(None, {'search_results': r})
Document templates may be published directly with Bobo. Bobo treats document templates as if they were Python functions that accept the arguments named ' self ' and ' REQUEST '. The object traversed to get to the document template is passed as the ' self ' argument and the request object is passed as the ' REQUEST ' argument. Typically, document templates are defined as class attributes and passed class instances and request data, so instance and request data can be used to generate text.
Document templates may also be used in Python functions called by document templates, as in:
def getResults(self, key, REQUEST):
result_data=self.search(key)
return self.resultTemplate(seld, REQUEST, search_results=result_data)
Be sure to call the document template. A common mistake is to return the document template directly, as in:
def getResults(self, key, REQUEST):
result_data=self.search(key)
return self.resultTemplate
Bobo does not attempt to call an object returned from a published object. Results of calling a published function are simply converted to strings and returned. When a document template is converted to a string, the document template source is returned. In the example above, the document template source, rather than the rendered template is returned.
The var tag is used to perform simple variable substitutions. A number of attributes are provided to control how text are inserted and formatted. The attributes are summarized in table See var tag attributes
The name of the variable to be inserted. See The name attribute |
||
An expression that evaluates to the value to be inserted. See The expr attribute |
||
Specify a data format, which may be a special, custom, or C-style format. See Custom, special, C, and empty formats |
||
Specify a string to be inserted for null values. See Null Values |
||
Cause the first character of the inserted value to be converted to upper case. |
||
Cause underscores in the inserted value to be converted to spaces. |
||
Cause commas to be inserted every three digits to the left of a decimal point in values containing numbers. For example, the value, "12000 widgets" becomes "12,000 widgets". |
||
Convert characters that have special meaning in HTML to HTML character entities. |
||
Convert characters that have special meaning in URLS to HTML character entities using decimal values. |
||
Convert single quotes to pairs of single quotes. This is needed to safely include values in Standard Query Language (SQL) strings. |
||
Convert new-line characters, carriage-return characters, and new-line-carriage-return character combinations to new-line characters followed by HTML break tags. |
||
Truncate the value to the given size. See Truncation |
||
Provide a string to be added to truncated text to indicate that truncation has occurred. The default value is "...". See Truncation |
A custom format can be used when outputting some objects. The value of a custom format is a method name to be invoked on the object being inserted. The method should return an object that, when converted to a string, yields the desired text. For example, the DTML source text:
<!--#var date fmt=DayOfWeek-->
Inserts the result of calling the method DayOfWeek of the value of the variable date , with no arguments.
In addition to custom formats, a few special formats are defined by the var tag that can be used with the fmt attribute. These are summarized in table See Special formats that may be used with the var tag fmt attribute .
Show a numeric value with a dollar symbol and two decimal places. |
|
In addition to custom and special formats, C 7 -style formats may also be used. A C-style format consists of text containing a single conversion specification. A conversion specification consists of a percent sign, optionally followed by a flag, a field width, a precision, and a conversion specifiers. A description of C-style formats is beyond the scope of this document. For details on C-style formats, see a C-language reference manual. Not all conversion specifiers are supported by DTML. Table See C-style specifiers for the fmt attribute summarizes the conversion specifiers that DTML supports.
In some applications, and especially in database applications, data variables may alternate between "good" and "null" or "missing" values. A format that is used for good values may be inappropriate for null values. For this reason, the null attribute can be used to specify text to be used for null values. Null values are defined as values that:
For example, when showing a monetary value retrieved from a database that is either a number or a missing value, the following variable insertion might be used:
The attributes size and etc can be used to truncate long strings. If the size attribute is specified, the string to be inserted is truncated at the given length. If a space occurs in the second half of the truncated string, then the string is further truncated to the right-most space. After truncation, the value given for the etc attribute is added to the string. If the etc attribute is not provided, then " ... " is used. For example, if the value of the variable spam is " blah blah blah blah ", then the tag:
Sometimes, the text to be included in a document is dependent on some data. The if tag is provided to support the conditional insertion of text based on DTML variables or expressions. As described in See DTML Tag Syntax , the if tag has four forms:
The if tag works in a straightforward manner. The variable or expression given in the if tag is evaluated. If the variable or expression value is true, the text following the if tag is inserted. If the variable or expression value is false, then for each elif tag given, the expression or variable given in the elif tag is evaluated, in the order given. If an elif variable or expression value is true, the text following the elif tag is inserted and none of the following elif variables or expressions are evaluated.. If there are no elif tags or if all of the elif tag variables or expression values are false, then the text following the else tag is inserted. If no else tag was supplied, then no text is inserted.
The if and elif tags support only the standard name and expr attributes. The else tag accepts no attributes.
In addition to the if tag, an unless tag is provided, with a associated closing tag, /unless , to insert text if a condition is false. Like the if tag, the unless tag accepts the standard name and expr attributes:
<!--#unless input_name-->
You did not provide a name.
<!--#/unless-->
It is sometimes necessary to insert a sequence of values.
The in tag is used to iterate over a sequence of objects. For example, an employee directory listing might be created with DTML source like that shown in figure See DTML source to create an employee phone listing . In this example, employees is either a sequence of employees, or a function that computes a sequence of employees. Each employee has name and phone attributes. These attributes are accessed using var tags. An in -tag sort attribute is used to sort employees by name in the output. The in tag attributes are listed in table See in tag attributes .
The name of the variable to be inserted. See The name attribute |
||
An expression that evaluates to the value to be inserted. See The expr attribute |
||
Normally, the attributes of items in the sequence are displayed. Sometimes, the items should be treated as mapping objects, meaning that items are to be looked up. |
||
The sort attribute is used to cause a sequence of objects to be sorted before text insertion is performed. The attribute value is the name of the attribute (or key if the mapping attribute was provided) that items should be sorted on. |
||
The name of a (request) variable that specifies the number of the row to start a batch on 8 . |
||
If the previous attribute is included, then iterative insertion will not be performed. The text following the in tag will be inserted and batch processinga variables associated with information about a previous batch will be made available. |
||
The next attribute has the same meaning and use as the previous attribute, except that variables associated with the next batch are provideda. |
In the previous example, an empty table would be displayed if there were no employees. It might be better to avoid showing an empty table and simply provide a message indicating that there are no employees. This can be done using the in tag in combination with the if tag, as shown in figure See DTML source to create an employee phone listing, while properly handling the case of no employees, using an in tag with an if tag. . In this example, the if tag is used with the employees variable. Sequences of objects are false if they are empty and true if they are not. If there are no employees, the condition in the if tag is false and the text following the else tag is inserted.
In the previous example (figure See DTML source to create an employee phone listing, while properly handling the case of no employees, using an in tag with an if tag. ), an in tag is combined with an if tag to avoid showing an empty table if a sequence of employees was empty. An alternative approach is to use an intermediate else tag in an in tag. If an in tag has an intermediate else tag, then the text following the else tag is inserted if the sequence used in the in tag is empty. Figure See DTML source to create an employee phone listing, while properly handling the case of no employees, using an else tag in an in tag. shows DTML source that uses an else tag in the in tag to avoid showing an empty table. The output from this source is the same as the output from the source shown in figure See DTML source to create an employee phone listing, while properly handling the case of no employees, using an in tag with an if tag. . The source in figure See DTML source to create an employee phone listing, while properly handling the case of no employees, using an else tag in an in tag. is actually more complex that the source in figure See DTML source to create an employee phone listing, while properly handling the case of no employees, using an in tag with an if tag. . The added complexity is due to the fact that the table header and footer had to be moved inside the in tag and the insertion of the table header and footer had to be conditioned on whether or not an item was the first item, last item, or neither, using the variables sequence-start and sequence-end (table See Item variables defined by the in tag ).
In most cases, it's best to use an in tag inside an if tag, as illustrated in figure See DTML source to create an employee phone listing, while properly handling the case of no employees, using an in tag with an if tag. . One case in which it may be best to use an else tag within an in tag is when the sequence used by the in tag is computed using an expr attribute and the computation is expensive. Use of the else tag in the in tag avoids having to define and evaluate the expression twice
When a name attribute is used in an in tag within an if tag, the sequence is only evaluated once, because the if tag caches the value associated with a name attribute.
When text is inserted with an in tag, a copy of the text is inserted for each item in the sequence. Tags in the inserted text have access to variables not available outside the in tag. These include:
In addition, for each of the variables listed in tables See Item variables defined by the in tag , See Batch-processing variables , and See Attributes of batch objects used when iterating over next-batches and previous-batches variables. with names ending in "-index", there are variables with names ending in "-number", "-roman", "-Roman", "-letter", and "-Letter" that are indexed from 1, "i", "I", "a", and "A", respectively. The sequence-index variable is used to number items as text is inserted. The variables like sequence-letter and sequence-roman provide for numbering using letters and Roman numerals.
Also, for each of the variables listed in tables See Item variables defined by the in tag , See Batch-processing variables , and See Attributes of batch objects used when iterating over next-batches and previous-batches variables. with names ending in "-index", there are variables with names ending in "-var-xxx", where "xxx" is an element attribute name or key. This is useful when displaying previous- and next-batch information. The construct is also useful in an if tag to test whether an attribute is present, because the attribute lookup will not be extended to the full document template name space.
The in tag provides variables (table See Summary statistic variables defined by the in tag ) for accessing summary statistics. Summary statistics are computed over the entire sequence, not just over items displayed.
total-nnn 9 |
|
The variance of numeric values computed with a degrees of freedom equal to the count - 1. |
|
The variance of numeric values computed with a degrees of freedom equal to the count. |
|
The standard deviation of numeric values computed with a degrees of freedom equal to the count - 1. |
|
The standard deviation of numeric values computed with a degrees of freedom equal to the count. |
When displaying a large number of objects, it may be impractical to display all of the data at once. While the approach used in figure See DTML source to create an employee phone listing, while properly handling the case of no employees, using an in tag with an if tag. is fine for a small group of employees, it is unacceptable for browsing the employees of a large company.
For this reason, the in tag provides support for batch processing. Information is displayed in batches. Variables are provided (table See Batch-processing variables ) to aid in the construction of HTML hyper-links to other batches.
The batch-processing facilities of the in tag are quite powerful, but the various options and approaches are complex enough that it is best to look at the batch-processing features by way of an example. The example chosen here is contrived to be extremely simple to make it as easy as possible to follow what's going on. Consider a table of 36 words (figure See Table of 36 words ) We will start by using the DTML source in figure See DTML source to browse 36 words, 5 words at a time to display this data. The DTML uses an if tag to test for an empty sequence of words. The actual sequence is named w36 . Inside the if tag, there are three in tags. All three in tags include the attributes size, with the value 5, and start , with the value qs . The size attribute is used to specify a batch size. Normally, one would not choose such a small batch size, but a small batch size helps to make the presentation shorter. The start parameter is used to specify a name of a variable that holds the index of the first element of the sequence to be displayed. If the variable is not defined, then the first batch is displayed. Figure See The output of the DTML source in figure 5 as displayed in a Web browser for several batches. shows the output of the DTML as displayed on a Web browser for the first two and last two batches..
The first of the three
in
tags is used to display an HTML hyper-link to a previous batch of data. The
previous
attribute in the
in
tag indicates that only previous-batch data should be displayed. Row data are not displayed. If the first batch is being displayed, then no text is inserted, as can be seen in figure 7 (words 1-5). The source in the first
in
tag uses four variable references. The first retrieves the
document_id
, which is used as a relative URL name for the document. The second variable reference, using variable
sequence-query
, retrieves the request query string that has been modified so that it does not include the variable named in the
in
tag
start
attribute,
qs
. The
sequence-query
value also contains punctuation, `
?
' and `
&
' necessary so that the
document_id
,
sequence-query
, and URL-encoded value for the next batch start can be concatenated. The URL-encoded value of the next batch start is "
qs=
" followed by the variable,
previous-sequence-start-number
. The variable
previous-sequence-size
provides the size of the previous batch for display in the hyper-link. Note that the previous (or next) sequence size is not necessarily equal to the batch size.
Note that the DTML source has been split over multiple lines by introducing line breaks within var tags. This is a useful way to break up long lines without causing line-breaks to be included in generated HTML.
The second in tag simply displays the rows in the batch. The third in tag is similar to the first in tag, except that a hyper link to the next, rather than the previous batch is displayed. Table See Query strings, and previous batch URL and next batch URL for the batches shown in figure 6. shows the query string, previous batch URL and next-batch URL for the example shown in figure See The output of the DTML source in figure 5 as displayed in a Web browser for several batches. .
Note that the size of the last batch is six. This is because the in tag has a feature that attempts to prevent the display of very small batches by combining them with adjacent batches. Normally, if the number of rows in a batch is less than or equal to two, then it is combined with an adjacent batch. The orphan attribute in the in tag can be used to provide an alternate setting. The value provided in the orphan attribute is the desired minimum batch size. The default setting for orphan is 3.
Normally, batches are non-overlapping. For large batch sizes, it is sometimes useful to overlap rows between batches. The overlap attribute in the in tag is used to specify how many rows to overlap between batches. The default is 0.
The variables beginning previous-sequence-start- , previous-sequence-end- , next-sequence-start- , and next-sequence-end- and ending in index , number , roman , Roman , letter , Letter , and var-xxx , where xxx is a row attribute (or key) name can be used to label which rows begin and end previous and next batches. This is illustrated in figures See Using batch-processing variables to show previous and next batch row number using Roman and Arabic numerals. and See Using batch-processing variables to show previous-batch row number using letters and next-batch starting and ending words. , which use various batch insertion variables to label previous and next batches.
Hyper links to multiple batches can be provided using the next-batches and previous-batches variables. These variables provide access to sequences of mapping objects containing information about all previous and next batches. Figure See Use of DTML to provide links to all previous and next batches. provides examples of using previous-batches to include hyper links to previous batches showing starting and ending row numbers, and of using next-batches to show hyper-links to following batches showing starting and ending words. Note that nested in tags are used to iterate over batch information. The nested in tags must use the mapping attribute, because the items in the sequences associated with next-batches and previous-batches are mapping objects.
The with tag can be used to expand the name space of a document template by adding attributes (or mapping keys) from an object that is already in the document template name space. For example, if a document template is used to display a folder, the contents of a sub-folder can be displayed using a with tag:
<!--#with subfolder-->
<!--#var title-->
<!--#/with-->
In combination with the namespace method of the special variable, _, the with tag can be used to add new variables to the DTML name space:
<!--#with "_.namespace(profit=price-cost, title=product_name+' summary')"-->
<h3><!--#var title--></h3>
The profit is <!--#var profit-->
<!--#/with-->
A common use of the with tag is to cause request variables to be used before object attributes:
The current id is <!--#var id-->
<!--#with REQUEST-->
The id you entered was <!--#var id-->
<!--#/with-->
Normally, document templates are used as methods of objects. Object attributes normally take precedence over request variables. Using the REQUEST variable in a with tag causes the request to be searched before other parts of the name space.
Sometimes, document templates are used to perform actions in addition to or even instead of displaying results. Methods can be called when evaluating name attributes or in expr attribute expressions. These methods may perform a useful action but produce no output or produce an output that is not needed. The call tag is provided for evaluating expressions or calling methods that have a useful side effect without inserting any text:
<!--#call "addDocument('hi','display a greeting','Hello world!')"-->
In many applications, inputs or other variables need to be checked for validity before actions are performed. DTML provides a convenient means of performing validity checks using the raise tag in combination with the if tag. Validity checks are performed with the if tag. The raise tag is used to report errors.
The raise tag has a type attribute for specifying an error type. Like the standard name attribute, the attribute name of the type attribute may be omitted. The error type is a short descriptive name for the error. In addition, there are some standard types, like " Unauthorized " and " Redirect " that are returned as HTTP errors. In particular, " Unauthorized " errors cause a log-in prompt to be displayed on the user's browser.
The raise tag is an non-empty tag. The source enclosed by the raise tag is rendered to create an error message. If the rendered text contains any HTML markup, then Bobo will display the text as an error message on the browser, otherwise a generic error message is displayed.
<!--#if "balance >= debit_amount"-->
<!--#call "debitAccount(account)"-->
Your account, <!--#var account-->, has been debited.
<!--#else-->
<!--#raise type="Insufficient funds"-->
There is not enough money in account <!--#account-->
to cover the requested debit amount.<p>
<!--#/raise-->
<!--#/if-->
The raise tag causes a Bobo error to occur. This has the important side affect that any changes made by a web request are rolled back, assuming that a transactional persistence mechanism, like BoboPOS is being used.
The comment tag provides a way to exclude source text from the rendered text. The comment tag is a simple non-empty tag that inserts no text. Further, the source enclosed by a comment tag is not rendered. Comment tags can be used to provide explanatory text or to disable parts of the source. Comment tags may be nested. Here is an example:
<!--#call updateData-->
The data have been updated.
<!--#comment-->
This comment is used to disable logging.
<!--#comment-->The following call records that updates were made
<!--#/comments-->
<!--#call logUpdates-->
<!--#/comment-->
abs method of the _ special variable used in expr attribute expressions 6
acos method of the math attribute of the _ special variable used in expr attribute expressions 7
appendHeader request response method 12
asin method of the math attribute of the _ special variable used in expr attribute expressions 7
atan method of the math attribute of the _ special variable used in expr attribute expressions 7
atan2 method math attribute of the _ special variable used in expr attribute expressions 7
atof method of the string attribute of the _ special variable used in expr attribute expressions 8
atoi method of the string attribute of the _ special variable used in expr attribute expressions 8
AUTH_TYPE request variable defined by the Common Gateway Interface (CGI) Specification 13
capitalize attribute of the var tag 3, 16
capitalize method of the string attribute of the _ special variable used in expr attribute expressions 8
capwords method of the string attribute of the _ special variable used in expr attribute expressions 8
ceil method of the math attribute of the _ special variable used in expr attribute expressions 7
center method of the string attribute of the _ special variable used in expr attribute expressions 9
CGI-defined request variables 13
choice method of the whrandom attribute of the _ special variable used in expr attribute expressions 9
chr method of the _ special variable used in expr attribute expressions 6
collection-length special format for the fmt attribute of the var tag 17
CONTENT_LENGTH request variable defined by the Common Gateway Interface (CGI) Specification 13
CONTENT_TYPE request variable defined by the Common Gateway Interface (CGI) Specification 13
cos method of the math attribute of the _ special variable used in expr attribute expressions 7
cosh method of the math attribute of the _ special variable used in expr attribute expressions 7
count method of the string attribute of the _ special variable used in expr attribute expressions 9
count-nnn sequence summary statistic 23
digits attribute of the string attribute of the _ special variable used in expr attribute expressions 8
divmod method of the _ special variable used in expr attribute expressions 6
dollars-and-cents special format for the fmt attribute of the var tag 17
e attribute of the math attribute of the _ special variable used in expr attribute expressions 7
etc attribute of the var tag 16, 18
exp method of the math attribute of the _ special variable used in expr attribute expressions 7
expireCookie request response method 12
expr attribute, special "_" variable 6
fabs method of the math attribute of the _ special variable used in expr attribute expressions 7
find method of the string attribute of the _ special variable used in expr attribute expressions 8
float method of the _ special variable used in expr attribute expressions 6
floor method of the math attribute of the _ special variable used in expr attribute expressions 7
fmod method of the math attribute of the _ special variable used in expr attribute expressions 7
fmt attribute of the var tag 16
frexp method of the math attribute of the _ special variable used in expr attribute expressions 8
GATEWAY_INTERFACE request variable defined by the Common Gateway Interface (CGI) Specification 13
getattr method of the _ special variable used in expr attribute expressions 6, 10
getHeader request response method 12
getitem method of the _ special variable used in expr attribute expressions 6
hash method of the _ special variable used in expr attribute expressions 6
hex method of the _ special variable used in expr attribute expressions 6
hexdigits attribute of the string attribute of the _ special variable used in expr attribute expressions 8
html_quote attribute of the var tag 16
hypot method of the math attribute of the _ special variable used in expr attribute expressions 8
index method of the string attribute of the _ special variable used in expr attribute expressions 9
int method of the _ special variable used in expr attribute expressions 6
join method of the string attribute of the _ special variable used in expr attribute expressions 9
ldexp method of the math attribute of the _ special variable used in expr attribute expressions 8
len method of the _ special variable used in expr attribute expressions 6
letters attribute of the string attribute of the _ special variable used in expr attribute expressions 8
ljust method of the string attribute of the _ special variable used in expr attribute expressions 9
log method of the math attribute of the _ special variable used in expr attribute expressions 8
log10 method of the math attribute of the _ special variable used in expr attribute expressions 8
lower attribute of the var tag 16
lower method of the string attribute of the _ special variable used in expr attribute expressions 9
lowercase attribute of the string attribute of the _ special variable used in expr attribute expressions 8
lstrip method of the string attribute of the _ special variable used in expr attribute expressions 9
maketrans method of the string attribute of the _ special variable used in expr attribute expressions 9
mapping attribute of the in tag 20
math attribute of the _ special variable used in expr attribute expressions 7
max method of the _ special variable used in expr attribute expressions 7
max-nnn sequence summary statistic 23
mean-nnn sequence summary statistic 23
median-nnn sequence summary statistic 23
min method of the _ special variable used in expr attribute expressions 7
min-nnn sequence summary statistic 23
modf method of the math attribute of the _ special variable used in expr attribute expressions 8
name attribute, cached values 5
namespace method of the _ special variable used in expr attribute expressions 7
namespace method of the special variable _ 30
newline_to_br attribute of the var tag 16
next attribute of the in tag 20
next-batches batch processing variable 24
next-sequence batch processing variable 24
next-sequence-end-index batch processing variable 24
next-sequence-size batch processing variable 24
next-sequence-start-index batch processing variable 24
None attribute of the _ special variable used in expr attribute expressions 7
oct method of the _ special variable used in expr attribute expressions 7
octdigits method of the string attribute of the _ special variable used in expr attribute expressions 8
ord method of the _ special variable used in expr attribute expressions 7
PATH_INFO request variable defined by the Common Gateway Interface (CGI) Specification 13
PATH_TRANSLATED request variable defined by the Common Gateway Interface (CGI) Specification 13
pi attribute of the math attribute of the _ special variable used in expr attribute expressions 8
pow method of the _ special variable used in expr attribute expressions 7
pow method of the math attribute of the _ special variable used in expr attribute expressions 8
previous attribute of the in tag 20
previous-batches batch processing variable 24
previous-sequence batch processing variable 24
previous-sequence-end-index batch processing variable 24
randint method of the whrandom attribute of the _ special variable used in expr attribute expressions 9
random method of the whrandom attribute of the _ special variable used in expr attribute expressions 9
redirect request response method 12
REMOTE_ADDR request variable defined by the Common Gateway Interface (CGI) Specification 13
REMOTE_HOST request variable defined by the Common Gateway Interface (CGI) Specification 13
REMOTE_IDENT request variable defined by the Common Gateway Interface (CGI) Specification 13
REMOTE_USER request variable defined by the Common Gateway Interface (CGI) Specification 13
REQUEST_METHOD request variable defined by the Common Gateway Interface (CGI) Specification 13
rfind method of the string attribute of the _ special variable used in expr attribute expressions 8
rindex method of the string attribute of the _ special variable used in expr attribute expressions 9
rjust method of the string attribute of the _ special variable used in expr attribute expressions 9
round method of the _ special variable used in expr attribute expressions 7
rstrip method of the string attribute of the _ special variable used in expr attribute expressions 9
SCRIPT_NAME request variable defined by the Common Gateway Interface (CGI) Specification 13
seed method of the whrandom attribute of the _ special variable used in expr attribute expressions 9
sequence variables defined by the in tag 22
sequence-end variable created by the in tag 22
sequence-index variable created by the in tag 22
sequence-item variable created by the in tag 22
sequence-key variable created by the in tag 22
sequence-query batch processing variable 24
sequence-start variable created by the in tag 22
sequence-step-size batch processing variable 24
SERVER_NAME request variable defined by the Common Gateway Interface (CGI) Specification 13
SERVER_PORT request variable defined by the Common Gateway Interface (CGI) Specification 13
SERVER_PROTOCOL request variable defined by the Common Gateway Interface (CGI) Specification 13
SERVER_SOFTWARE request variable defined by the Common Gateway Interface (CGI) Specification 13
setCookie request response method 12
setHeader request response method 12
setStatus request response method 12
short-hand form of the expr attribute 2
short-hand version of the name attribute 2
sin method of the math attribute of the _ special variable used in expr attribute expressions 8
sinh method of the math attribute of the _ special variable used in expr attribute expressions 8
size attribute of the in tag 20
size attribute of the var tag 16, 18
sort attribute of the in tag 20
spacify attribute of the var tag 16
split method of the string attribute of the _ special variable used in expr attribute expressions 9
sql_quote attribute of the var tag 16
sqrt method of the math attribute of the _ special variable used in expr attribute expressions 8
standard-deviation-nnn sequence summary statistic 23
standard-deviation-n-nnn sequence summary statistic 23
start attribute of the in tag 20
str method of the _ special variable used in expr attribute expressions 7
string attribute of the _ special variable used in expr attribute expressions 7, 8
strip method of the string attribute of the _ special variable used in expr attribute expressions 9
Summary statistic variables defined by the in tag 23
swapcase method of the string attribute of the _ special variable used in expr attribute expressions 9
tan method of the math attribute of the _ special variable used in expr attribute expressions 8
tanh method of the math attribute of the _ special variable used in expr attribute expressions 8
thousands_commas attribute of the var tag 16
total-nnn sequence summary statistic 23
translate method of the string attribute of the _ special variable used in expr attribute expressions 9
uniform method of the whrandom attribute of the _ special variable used in expr attribute expressions 9
upper attribute of the var tag 16
upper method of the string attribute of the _ special variable used in expr attribute expressions 9
uppercase attribute of the string attribute of the _ special variable used in expr attribute expressions 8
1. It is also possible to define additional syntaxes, although the mechanism for doing this is not currently documented. For example, a syntax that is similar to the syntax used by active server pages has been developed.
2. Actually, all attributes have values, but some attributes, like the capitalize attribute have defaults and the values can be omitted. Many attributes, like the capitalize attributes are flags. Their presence typically indicates that some option that is normally disabled should be enabled. There is one case in which values must be provided for these attributes, that being when the attribute is the first attribute given for a tag and would be confused as a name attribute value. For attributes like capitalize, a value of "yes", "on", or 1 is uasually provided, as in: <!--#var capitalize=1 name=id-->
4. A method is like a function except that it is an attribute of an object and may use the object's data in computation.
6. Python 1.4 users must use the ni module to enable packages. DocumentTemplate may also be used as a collection of modules, rather than as a package by copying all of the DocumentTemplate modules except the __init__ module to a directory in the Python path.