detox
– Detox templates
This module is an XIST namespace. It provides a simple template language based on processing instructions embedded in XML or plain text.
The following example is a simple “Hello, World” type template:
from ll.xist.ns import detox
template = """
<?def helloworld(n=10)?>
<?for i in range(n)?>
Hello, World!
<?end for?>
<?end def?>
"""
module = detox.xml2mod(template)
print "".join(module.helloworld())
- class ll.xist.ns.detox.code[source]
Bases:
ProcInst
Embed the PI data literally in the generated code.
For example
<?code foo = 42?>
will put the statementfoo = 42
into the generated Python source.
- class ll.xist.ns.detox.if_[source]
Bases:
ProcInst
Starts an if block. An if block can contain zero or more
elif_
blocks, followed by zero or oneelse_
block and must be closed with anend
PI.For example:
<?code import random?> <?code n = random.choice("123?")?> <?if n == "1"?> One <?elif n == "2"?> Two <?elif n == "3"?> Three <?else?> Something else <?end if?>
- class ll.xist.ns.detox.def_[source]
Bases:
ProcInst
Start a function (or method) definition. A function definition must be closed with an
end
PI.Example:
<?def persontable(persons)?> <table> <tr> <th>first name</th> <th>last name</th> </tr> <?for person in persons?> <tr> <td><?textexpr person.firstname?></td> <td><?textexpr person.lastname?></td> </tr> <?end for?> </table> <?end def?>
If the generated function contains output (i.e. if there is text content or
expr
,textexpr
orattrexpr
PIs before the matchingend
) the generated function will be a generator function.Output outside of a function definition will be ignored.
- class ll.xist.ns.detox.class_[source]
Bases:
ProcInst
Start a class definition. A class definition must be closed with an
end
PI.Example:
<?class mylist(list)?> <?def output(self)?> <ul> <?for item in self?> <li><?textexpr item?></li> <?endfor?> </ul> <?end def?> <?end class?>
- class ll.xist.ns.detox.for_[source]
Bases:
ProcInst
Start a
for
loop. A for loop must be closed with anend
PI.For example:
<ul> <?for i in range(10)?> <li><?expr str(i)?></li> <?end for?> </ul>