Genshi ====== >>> from z3c.pt.testing import render_genshi py:if >>> print render_genshi("""\ ...
...
...

Bar

...
...
...

Foo

...
... ... Bar ... ... ... Foo ... ...
""")

Foo

Foo
py:choose, py:when, py:otherwise >>> print render_genshi("""\ ...
...
... 0 ... 1 ...
... 2 ... 3 ...
... 3 ... 4 ...
...
... 3 ...
... 1 ... 1 ...
...
... 1 ... 2 ...
...
...
""")
1
2
3
1
2
py:for >>> print render_genshi("""\ ...

...
- ${item}
... ...
- ${item}
... ...

""")

- 0
- 1
- 2
- 3
- 4

py:def >>> print render_genshi("""\ ...
...

... Hello, ${name}! ...

... ${greeting('world')} ... ${greeting('everyone else')} ... ...

Goodbye, ${name}!

... ... ${goodbye('world')} ... ${goodbye('everyone')} ...
""")

Hello, world!

Hello, everyone else!

Goodbye, world!

Goodbye, everyone!

py:with >>> def quote(): ... return dict(author=u"Some name", quote=u"Some random quote") >>> print render_genshi("""\ ...
... ${x} ${y} ${z} ... ${x} ${y} ${z} ...

> ... "${q["quote"]} ${q["author"]} ...

...
""", quote=quote)
2 7 12 4 3 9

> "Some random quote Some name

py:attrs >>> print render_genshi("""\ ...

...
- Bar
...
- Bar
...

""", d=dict({'class': u'\u1234'}))

- Bar
- Bar

py:content, py:replace >>> print render_genshi("""\ ...
... ... ...
""")
Hello, world! Goodbye, world!
py:strip >>> print render_genshi("""\ ...
... foo ...
""")
foo
py:match >>> print render_genshi("""\ ...
... ... Hello, ${select('@name')[0]}! ... ... ... Goodbye, ${select('@name')[0]}! ... ... ... ...
""")
Hello, dude! Goodbye, dude!
:: Genshi variable interpolation (${} notation) >>> print render_genshi("""\ ...
... inter${'pol' + 'ati'}onis ${int('test') | 'convenient'}! ... ${'a'}${'b'}${'c'} ${'d'} ... ... ... ...
""")
interpolationis convenient! abc d
:: Simple variable names do not need { .. }. From `py:with specs`_. >>> print render_genshi("""\ ...
... $x $y $z ...
""", x=42)
42 7 52
:: Genshi variable interpolation and unicode values >>> print render_genshi("""\ ...
... ... ... ... ${unicode('La Pe\xc3\xb1a', 'utf-8').encode('utf-8')} ... ... ...
""")
La Peña
:: Unicode combined with attribute expansion >>> print render_genshi("""\ ...
... ... ...
""", alt=unicode("La Pe\xc3\xb1a", 'utf-8'))
:: Variables containing quotes >>> print render_genshi("""\ ...
... "${quote}" ...
""", quote="Hello, World!")
"Hello, World!"
:: Variables containing markup >>> print render_genshi("""\ ...
... ${message} ...
""", message="Hello, World!")
Hello, World!
:: Variable expansion must preserve XML validity >>> from z3c.pt import config >>> config.VALIDATION = True >>> print render_genshi("""\ ...
... ${message} ...
""", message="Hello, World!") Traceback (most recent call last): ... ExpatError: ... >>> config.VALIDATION = False :: Unless we are in a CDATA block >>> print render_genshi("""\ ...
... /* */ ...
""", message="Hello, World!")
/* */
:: z3c.pt used to forget to output the ]; after the for loop >>> print render_genshi("""\ ...
""")
:: Strange looping behaviour >>> brands=[dict(id=1, title="One"), ... dict(id=2, title="Two"), ... dict(id=3, title="Three")] >>> print render_genshi("""\ ...
...
...
""", brands=brands)
:: Very basic recursing >>> print render_genshi("""\ ...
...

... ${rendermenu([])} ...

...
""")
:: Slightly more complex recursive function calls >>> print render_genshi("""\ ...
...

...
- ... Merken ... ${rendermenu(entry.get('children', []))} ...
...

... ${rendermenu(menu)} ...
""", menu=[dict(title=u"Menu entry", url="/test", current=True)])

- Menu entry

.. _py:with specs: http://genshi.edgewall.org/wiki/Documentation/0.4.x/xml-templates.html#py-with