logo       

Re: the "Cheetah files" (part 1): msg#00010

python.cheetah

Subject: Re: the "Cheetah files" (part 1)

On Thu, Oct 02, 2003 at 01:07:29PM +0200, deelan wrote:
> #if $seq
> <ul>
> #for $item in $seq
> <li><a href="read?id=$item.id">$item.title</a></li>
> #end for
> </ul>
> #else
> <p class="info">No message found.</p>
> #end if
>
> ugly and messy.

I've always done it this way. I think DTML has some special variables
to cover this situation, if I remember right. But whenever something
is "ugly", the easiest thing is to encapsulate it in a method:

$html_list([
"Item 1",
"Item 2",
], "<P> No message found,</P>")

where the method might be:

#def html_list($items, $messageIfNone=None, $listType='U'>
## $listType may be 'U' or 'O'.
#if $items
<${listType}L>
#for $item in $items
<LI> $item
#end for $item
</${listType}L>
#elif $messageIfNone
$messageIfNone
#end if $items
#end def html_list


I would also use a method to generate the hyperlinks. My favorite is:

def href(url, label, quoteLabel=True, **attributes):
"""If both 'url' and 'label', return a typical hyperlink.
If 'url' but not 'label', make the label identical to the URL.
If 'label' but not 'url', return the label instead of a hyperlink
(assume the link is disabled or not implemented yet).
If 'quoteLabel', HTML-encode the label. Otherwise assume it's
already encoded or doesn't need it.
If '**attributes', convert to hyperlink attributes, URL-encoding
both the key and the value.
"""
if url and label:
pass
elif url:
label = url
elif label:
return label
else:
return ''
if attributes:
atList = []
for key, value in attributes.iteritems():
tup = quoteUrl(key), quoteUrl(value)
s = '%s="%s"' % tup
atList.append(tup)
atString = ' '.join(atList)
else:
atString = ''
#url = quoteUrl(url)
if quoteLabel:
label = quoteHtml(label)
return '<A HREF="%s" %s>%s</A>' % (url, atString, label)

Here are a few other functions I use frequently:

def mailto(address, label=None):
label = label or address
if '@' in label and '@' not in address:
raise ValueError("arguments in wrong order")
return '<A HREF="mailto:%s";>%s</A>' % (address, label)

def chattyMailto(address, label):
if '@' in label and '@' not in address:
raise ValueError("arguments in wrong order")
return '%s &lt;<A HREF="mailto:%s";>%s</A>&gt;' % (label, address, address)


def menu1(*items):
"""A simple but elegant horizontal text menu (normally of hyperlinks)."""
return "&nbsp;|&nbsp;\n".join(items)

def anchor(name, quote=True):
"""Make the target of a "#fragment" URL."""
if quote:
name = quoteUrl(name)
return '<A NAME="%s"></A>' % name



--
-Mike Orr (aka. Sluggo), mso@xxxxxx (iron@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx)
http://iron.cx/ English * Esperanto * Russkiy * Deutsch * Espan~ol


-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf


<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise