[ZPT] Lower case

Jeffrey P Shell jeffrey@cuemedia.com
Tue, 25 Jun 2002 13:08:46 -0600


On 6/25/02 12:37 PM, "Colin Fox" <cfox@cfconsulting.ca> wrote:

> Hi everyone.
> 
> I need to be able to force the contents of a variable into lower case,
> and I can't find any docs on how to do this.
> 
> Here's a simple template showing an example of what I'm trying to do:
> 
> 
> <html>
> <head>
>   <title tal:content="template/title">The title</title>
> </head>
> <body>
>   
>   <span tal:define="b python:string.lower(${request/somevalue})">
>   <p tal:content="string:${b}">testing</p>
>   </span>
> 
> </body>
> </html>
> 
> 
> Any suggestions?

If you're using a Zope running Python 2.0 or later (ie, Zope 2.4 or later),
you can use string methods directly:

<span tal:define="b python:request.somevalue.lower()">
<p tal:content="b">testing</p>
</span>

If you're using older Python, or otherwise have the need, you need to access
the Python 'string' module out of the ZPT 'modules' context:

<span tal:define="b python:modules['string'].lower(request.somevalue)">
...
</span>

This is because Page Templates has built in Python functions that allow
usage of certain TALES expressions inside of Python expressions.  Since
'string' is the name of one of those TALES expression types, that's what is
bound to the default 'string' name used in Python expressions.  Thus, the
following two statements yield the same results:

<h2 tal:content="string:Hi ${request/name}!">Hi neighbor!</h2>
<h2 tal:content="python:string('Hi ${request/name}!')">Hi neighbor!</h2>

-- 
Jeffrey P Shell 
www.cuemedia.com