complaints and suggestions about template.py

9 views
Skip to first unread message

Anand

unread,
Nov 27, 2006, 3:56:12 AM11/27/06
to we...@googlegroups.com

I have some complaints on template.py, which have been bothering me
for quite some time and some suggestions for the same.

1. specifying globals in web.template.Template.globals is not nice.
What if i want to render templates from two different directories,
each with different set of globals?
Isn't it better to specify globals to the render function?

example:
render = web.template.render('templates/', globals=dict
(datestr=web.datestr))

2. using python style indentation in templating is sometimes painful.
Suppose i want to generate the following text.

<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ul>

if i use the following template

$ a = [1, 2, 3, 4]
<ul>
$for i in a:
<li>$i</li>
</ul>

it produces

<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ul>

But i missed the spaces before <li>. It doesnt matter much in most of
the web.py applications since we are dealing with html, but it is not
nice!
How about having $end? so the above template will become:

$ a = [1, 2, 3, 4]
<ul>
$for i in a:
<li>$i</li>
$end
</ul>

This preserves the whitespace and produces exactly what is desired.

anand

Aaron Swartz

unread,
Nov 27, 2006, 2:21:06 PM11/27/06
to we...@googlegroups.com
> 1. specifying globals in web.template.Template.globals is not nice.
> What if i want to render templates from two different directories,
> each with different set of globals?
> Isn't it better to specify globals to the render function?

Oops, yeah, I think you're right. Can you send me a patch that adds
this feature?

> 2. using python style indentation in templating is sometimes painful.
> Suppose i want to generate the following text.

Yeah, this bugs me too, but the $end statements bug me even more. I
sometimes wonder if maybe it should add the indentation to the output
as well, with maybe an option to suppress it.

Anand

unread,
Nov 27, 2006, 11:34:47 PM11/27/06
to we...@googlegroups.com
>
> Oops, yeah, I think you're right. Can you send me a patch that adds
> this feature?
>

here it is.

template.patch

Tommi

unread,
Nov 28, 2006, 8:13:24 AM11/28/06
to web.py
Could $for be sensitive to the leading spaces?

E.g. in...

<ul>
$for i in a:
<li>$i</li>
</ul>

...the parser would notice that it has some leading spaces and would
retain those during all iterations?

Aaron Swartz

unread,
Nov 28, 2006, 8:07:56 PM11/28/06
to we...@googlegroups.com
> ...the parser would notice that it has some leading spaces and would
> retain those during all iterations?

It's supposed to do that.

So:

<ul>
$for i in a: <li>$i</li>
</ul>

would be a sensible way of writing it.

Tommi

unread,
Dec 1, 2006, 5:05:25 AM12/1/06
to web.py
Aaron Swartz wrote:

> It's supposed to do that.

Oh, and it does! For some odd reason I thought it didn't. Thanks for
the correction.

I think there's still a minor issue with subtemplates...

<ul>
$for i in a: $:render.item(i)
</ul>

...if item.html happens to contain multiple lines.

Reply all
Reply to author
Forward
0 new messages