Forums: Help (Thread #21390)

python 2.3でのgenshi動作について (2009-01-21 13:24 by kabuchan #41360)

http://genshi.edgewall.org/ticket/181
結論はこれなのですが

<py:for></py:for>とかのブロックのなかの<?python ?>には
変数を持ち込むこともできないし
外に持ち出すこともできません

from genshi.template import MarkupTemplate
import datetime
cur = datetime.date(2009,1,19)
aaa = """
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:py="http://genshi.edgewall.org/">
<head>
</head>
<body>
<?python
from datetime import date, timedelta
aaa = 'local'
disp1 = locals().keys()
?>
<p>before py:for ...${str(disp1)}</p>
<py:for each="cnt in range(10)">
<?python
bbb = 'local in py:for'
disp2 = locals().keys()
?>
<p py:if="cnt==0">in py:for(disp2)... ${str(disp2)}</p>
<p py:if="cnt==0">in py:for(aaa) ... ${aaa}</p>
</py:for>
<?python
disp3 = locals().keys()
?>
<p>after py:for ...(disp3)${str(disp3)}</p>
</body>
</html>
"""
tmpl = MarkupTemplate( aaa )
stream = tmpl.generate(cur=cur)
print stream.render('xhtml')


これを実行すると
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
<p>before py:for ...['aaa', 'cur', 'timedelta', 'defined', 'value_of', 'date']</p>
<p>in py:for(disp2)... ['cnt', 'bbb']</p>
<p>in py:for(aaa) ... local</p>
<p>after py:for ...(disp3)['aaa', 'cur', 'timedelta', 'defined', 'value_of', 'date', 'disp1']</p>
</body>
</html>