questions about template: layout template, complex expression

3 views
Skip to first unread message

makoto kuwata

unread,
Oct 19, 2007, 3:28:09 PM10/19/07
to web.py
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?

(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

Adam Atlas

unread,
Oct 19, 2007, 3:33:01 PM10/19/07
to we...@googlegroups.com

On 19 Oct 2007, at 15:28, makoto kuwata wrote:
> $ color = i % 2 and 'pink' or 'gray' # unsupported operand
> type(s) for %: 'int' and 'str'

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.

makoto kuwata

unread,
Oct 19, 2007, 9:24:40 PM10/19/07
to web.py
Thank you, Adam.

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

makoto kuwata

unread,
Oct 19, 2007, 10:34:37 PM10/19/07
to web.py
Does anyone give me advices about layout template?

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

Adam Atlas

unread,
Oct 19, 2007, 10:38:49 PM10/19/07
to we...@googlegroups.com

On 19 Oct 2007, at 22:34, makoto kuwata wrote:
>
> Does anyone give me advices about layout template?

What does that mean?

makoto kuwata

unread,
Oct 20, 2007, 3:02:49 AM10/20/07
to web.py
> > (1) Does it support layout template feature?

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

David Terrell

unread,
Oct 30, 2007, 4:06:55 PM10/30/07
to we...@googlegroups.com

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/

Reply all
Reply to author
Forward
0 new messages