|
|
ARK template files
Syntax of the annotations:
- <% _suite_ %>
- Execute arbitrary suite of commands, may define page-global
variables and functions. Any output is included into the web page.
- <%= _expr_ [=]%>
- Evaluate expression and include result as text within the web page.
HTML special characters are escaped (e.g., '<' becomes '<').
- <%: _expr_ [:]%>
- Same as above except that the URL-special characters in the result
are escaped (e.g., ' ' becomes '%20').
- <%! _cmd_ [!]%>
- Special command annotation (see next section).
- if expr
- Begins a conditional block. The text between this and a closing
end (see below) command is included only if the expression
evaluates true.
- ifdef expr
- Begins a conditional block. The text between this and a closing
end is included only if the expression evaluates to the name
of a defined variable, e.g.:
<%! ifdef 'foo' %>
The foo is set to <%=foo=%>
<%! end ifdef %>
- for var in expr
- Begins a sequence block. The text between this and a closing
end are included for each item in the sequence evaluated to
by the expression. Each item is bound to the variable name in
turn. (Basically the same as a normal Python 'for' loop)
- with expr
- Temporarily binds the attributes of a module, object, or PostgreSQL
record as top-level variables. e.g., the following are equivalent:
<%= foo.bar %>
and:
<%! with foo %>
<%= bar %>
<%! end with %>
(Basically makes the contents of the __dict__ attribute visible)
- witheach expr
-
Combination of for and with designed for processing a sequence
of class-like objects. e.g., the following are equivalent:
<%! witheach thingies %>
...
<%! end witheach %>
and:
<%! for thing in thingies %>
<%! with thing %>
...
<%! end with %>
<%! end for %>
but the first is more concise and efficient.
- end ...
- Closes any of the above block commands. The keywords following 'end'
are entirely optional and are only there for readability.
- path directory
- Adds the given directory to the module searchpath.
To parse an annotated HTML file and produce a template:
page = ark.template.TemplateParser( <filename> )
or to parse a string containing annotated HTML:
page = ark.template.TemplateParser( text = <string> )
To execute the template and produce the final HTML as a string:
html = page.process( [<var> = <expr> [, <var> = <expr> ...]] )
The optional arguments supply variables that will be visible from
within the script. This includes modules and functions that you wish to
be visible, e.g.:
import re
def twice( x ):
return x + x
html = page.process( re = re, twice = twice )
|