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.expr[source]

Bases: ProcInst

Embed the value of the expression

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 statement foo = 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 one else_ block and must be closed with an end 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.elif_[source]

Bases: ProcInst

Starts an elif block.

class ll.xist.ns.detox.else_[source]

Bases: ProcInst

Starts an else block.

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 or attrexpr PIs before the matching end) 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 an end PI.

For example:

<ul>
        <?for i in range(10)?>
                <li><?expr str(i)?></li>
        <?end for?>
</ul>
class ll.xist.ns.detox.while_[source]

Bases: ProcInst

Start a while loop. A while loop must be closed with an end PI.

For example:

<?code i = 0?>
<ul>
        <?while True?>
                <li><?expr str(i)?><?code i += 1?></li>
                <?code if i > 10: break?>
        <?end while?>
</ul>
class ll.xist.ns.detox.end[source]

Bases: ProcInst

Ends a while_ or for_ loop or a if_, def_ or class_ block.