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.class_(*content)[source]

Bases: ll.xist.xsc.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.code(*content)[source]

Bases: ll.xist.xsc.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.def_(*content)[source]

Bases: ll.xist.xsc.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.elif_(*content)[source]

Bases: ll.xist.xsc.ProcInst

Starts an elif block.

class ll.xist.ns.detox.else_(*content)[source]

Bases: ll.xist.xsc.ProcInst

Starts an else block.

class ll.xist.ns.detox.end(*content)[source]

Bases: ll.xist.xsc.ProcInst

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

class ll.xist.ns.detox.expr(*content)[source]

Bases: ll.xist.xsc.ProcInst

Embed the value of the expression

class ll.xist.ns.detox.for_(*content)[source]

Bases: ll.xist.xsc.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.if_(*content)[source]

Bases: ll.xist.xsc.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 endif 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.while_(*content)[source]

Bases: ll.xist.xsc.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>