Chapter 5: CML Directives
[TOP] [PREV] [NEXT]
CML pages are like mini-programs. They contain directives which control which lines of HTML code will actually get sent to the browser, or control how many times a set of HTML lines will be evaluated.
The directives are:
Plus an "end" directive, that closes the "for", "count", "while", "if", "elif", and "else" directives.
The CML "for" loop evaluates a set of lines multiple times. It looks like:
for variable1 [variable2 ... ] in list (HTML code or other CML directive code) ... end
where variable1, variable2 etc. are names, and list is a list of words or values. Typically list may be the result of a CML function. The for loop evaluates the lines between "for" and "end", substituting the words in list as the values of variable1, variable2, etc. (The brackets simply mean that variable2, etc. are optional. The brackets would not actually appear in the syntax of the for loop.)
For example, the loop:
for x in abc qrs xy ... end
will evaluate the lines between "for" and "end" three times, using each word in list. (If there are no words, the lines will be skipped.) The first time through the loop, x will have as its value "abc". The second time it will have the value "qrs", and so on.
A different example shows the use of multiple variables:for one two in alpha beta delta gamma ... end
The first time through the loop, one will have the value "alpha" and two will have the value "beta". The second time, one will have the value "delta", and so on.
The indenting of each line as shown above is not necessary, but it is a good idea. It helps make the CML code much more readable.
The CML "count" loop is similar to the "for" loop. It looks like:
count variable x y (HTML code or other CML directive code) ... end
where variable is a name, and x and y are numeric values or expressions. The count loop will evaluate the lines between "count" and "end" one time for each integer value between x and y, inclusive. The first time, variable will have the value x. Then x+1, and so on, up to and including y. If y is less than x, the lines will be skipped entirely.
The CML "while" loop is perhaps the simplest loop control directive. It has the form:
while expression (HTML code or other CML directive code) ... end
The "while" loop evaluates expression, and examines the first word of the result. If it is a number, not equal to 0, all of the lines between "while" and "end" are evaluated. The loop then repeats, re-evaluating expression, and so on. The "while" loop will continue to execute as long as expression is non-zero, so be careful!
The CML "if" statement evaluates a set of lines if a certain condition is true. It looks like:
if condition (HTML code or other CML directive code) ... endwhere condition is some expression. If there is at least one word in condition, and the first word is a non-zero number, then the enclosed set of lines will be evaluated once. Otherwise they will be skipped. (Also see the related functions $if() and $ifthen().)
The "if" statement may be extended to handle multiple exclusive cases with the "elif" directive. It looks like:
if condition1 (HTML code or other CML directive code) ... end elif condition2 ... end
The lines between "elif" and "end" are evaluated when the previous "if" condition1 failed (was 0 or did not exist) and the first word of condition2 is a non-zero number.
Multiple "elif"s may be strung together, one after another. Only one of the blocks of CML code between the if/end and elif/end pairs will be executed.
There is an (optional) matching "else" to the CML "if" and "elif" statements. It looks like:
if condition (HTML code or other CML directive code) ... end else ... end
The lines between "else" and "end" are evaluated if condition is 0, or does not exist at all. Note: the "if" must have its own "end"! "Else" may be used with just an "if", or a series of "if"s and "elifs". If the latter, it must be the last of the series.
The "for" and "count" directives define the value of a variable during iterations of the lines between the "for" or "count", and the matching "end" directive. Outside of those loops, the variable is undefined.
A variable may also be defined across the evaluation of all CML pages, using the "set" directive. It looks like:
set variable xwhere variable is a name (or an expression that evaluates to a name), and x is some expression. For the rest of this session, variable has the value x (unless changed by another "set" directive). Variables defined by "set" are considered "global" in scope, i.e. the variables are available in all subsequently evaluated CML pages.
(Also see the related function $set().)
The Copy directive is identical to the Set directive, except that it preserves leading spaces (after the one space required to separate the directive from the expression). Spaces, in this context, include blanks, tabs, newlines, and Kanji double-wide spaces.
For example, if the "struck-out" letter
brepresents a space, then
bbbhello copy xyz bbbgoodbye
sets abc to "Hello", and xyz to "
The "include" directive includes the text of a CML file at the current point. It has the syntax:
include filename [ arg1 [ arg2 ... ] ]
where filename is the name of a file, or a set of CML functions that evaluate to the name of a file. Filename is relative to the CML_Path directory specified in the swebd.conf file. (See the Caucus installation guide for details.) The brackets indicate that arguments arg1, arg2, and so on are optional (they are not actually part of the syntax). If the arguments are present, they are available inside the included file via the $inc(n) function.
Include directives are evaluated according to the current context. For example:
count x 1 3 include file.$(x) end
would include the contents of the files file.1, file.2, and file.3.
Include understands the "quoting" of multiple words as one arg. See $quote() for details.
The "return" directive immediately "returns" from the current CML file or CML include file. When encountered in a CML file, it terminates processing of that file, as if it had "run off the end" of the file. If encountered in an included file, it immediately "returns" to the including file, and continues with the line after the include directive.
Return is particularly useful in CML include files that handle multiple conditions. Return can be used to say "I'm done with this section", rather than use if/else if combinations to exclusively handle different conditions. For example:
if condition 1 do something return end if condition 2 do something else return end "etc...A return in a main CML file has the same effect as a quit.
The "quit" directive immediately ceases processing of the current page, whether in a main CML or an include file. No more processing is done of that file, or any file that may have "included" the file.
It is particularly useful in CML pages that need to handle special case or "error" conditions. For example:
if some "error" condition "Location: http://www.xyz.com/errorpage.html " quit end #---OK, go on with the main case here... "Content-type: text/html " "etc...
The "break" directive immediately exits the innermost "for", "count", or "while" loop, and continues execution of the CML script after the closing "end" of that loop.
The "continue" directive immediately starts the next iteration of the innermost containing "for", "count", or "while" loop.
The "eval" directive simply evaluates any CML functions on the rest of the line. Any results from the evaluation are ignored (thrown away).
Eval is really just a more straight-forward way of saying "set ignore some_function".