(1) Does it support layout template feature?
(2) Is it available to put complex expression to the right-hand of
assignment?
In web.py 0.22, the following will be error.
--------------------
$def with (items)
<table>
$ i = 0
$ for item in items:
$ i = i + 1
$ color = i % 2 and 'pink' or 'gray' # unsupported operand
type(s) for %: 'int' and 'str'
<tr bgcolor="$:color">
<td>$item</td>
</tr>
$#
</table>
--------------------
The following is another example in which 'table["key"] = "VALUE"'
is
not recognized as assignment.
--------------------
$def with (errors)
$ table = {}
$ table["key"] = "VALUE"
--------------------
--
regards,
makoto kuwata
Change that to $ color = (i % 2) and 'pink' or 'gray'. The and/or
operators take precedence over the modulo operator, so your current
code is being evaluated like i % (2 and 'pink' or 'gray') (which will
always evaluate to i % 'pink' hence the error).
I just checked, and it looks like this is inconsistent with how
Python evaluates it. But for now, use the parentheses; all these
little anomalies should be gone in web.py 0.3.
But
$ color = (i % 2) and 'pink' or 'gray'
is seemed to be parsed as
$ color = (i % 2) and ('pink' or 'gray')
because color is toggled between 'pink' and 0.
The following
$ color = ((i % 2) and 'pink') or 'gray'
works expectedly.
--
regards,
makoto kuwata
On 2007-10-20 4:28, makoto kuwata <k...@kuwata-lab.com> wrote:
> Hi all, I'm new about web.py.
> I have two questions about template engine of web.py.
>
> (1) Does it support layout template feature?
>
--
regards,
makoto kuwata
What does that mean?
I found to get layout template.
The followings are example of layout template in web.py.
templates/layout.html:
--------------------
$def with (content_for_layout, title)
<html>
<body>
<h1>$title</h1>
<div id="maincontent">
$:content_for_layout
</div>
</body>
</html>
--------------------
template/item_list.html:
--------------------
$def with (items)
<table>
$ odd = False
$for item in items:
$ odd = not odd
$ color = (odd and 'pink') or 'gray'
<tr bgcolor="$:color">
<td>$item</td>
</tr>
$#
</table>
--------------------
code.py:
--------------------
title = 'Layout Template Example'
items = ['AAA', 'BBB', 'CCC']
import web
render = web.template.render('templates/')
output = render.item_list(items) ## render content
output = render.layout(output, title) ## render layout
print output,
--------------------
output:
--------------------
<html>
<body>
<h1>Layout Template Example</h1>
<div id="maincontent">
<table>
<tr bgcolor="pink">
<td>AAA</td>
</tr>
<tr bgcolor="gray">
<td>BBB</td>
</tr>
<tr bgcolor="pink">
<td>CCC</td>
</tr>
</table>
</div>
</body>
</html>
--------------------
--
regards,
makoto kuwata
Does: ('pink', 'gray')[i % 2] evaluate properly in the template?
Just as clear, less likely to fail.
--
David Terrell
d...@meat.net
((meatspace)) http://meat.net/