[Zope3-dev] Re: Better access to APIs in paths (was Re: needingviewsclues - template/title troubles)

Shane Hathaway shane@zope.com
Tue, 25 Feb 2003 15:07:07 -0500


Casey Duncan wrote:
> FWIW: Me Three.
> 
> I have no inherent love for XML namespaces, but they are a widely 
> understood/documented concept that we can readily co-opt for this.
> 
> Perhaps we could define some configurable default set of namespace 
> declarations made implicitly for all html page templates.
> 
> <beat what="drum">Either that or get the code the hell out of the templates 
> all together... ;^)</beat>

Well, part of what this does is it lets you do many things in templates 
that formerly required Python expressions.  Currency formatting is just 
scratching the surface.

And before we say that presentation details like formatting currency 
should require a Python script, imagine the following hypothetical 
future exchange:

=======================================

[Newbie]
I just downloaded Zope today and it's really cool.  I made a little 
checkbook register using a page template.  It's way better than PHP. 
Now I can't figure out how to show only two digits after the decimal 
point.  I don't know Python.  Can you help?

[Guru]
Hmm, you'll have to use some Python.  Don't use a Python expression, 
though, because that would be putting code in your template.  Write a 
script to do it.

[Newbie]
Can you help me write the script?

[Guru]
Ok, here you go:

from zope.app.servicenames import LocaleService
from zope.components import getService
def money(context, n):
   return getService(context, LocaleService).formatCurrency(n)

[Newbie]
Um, okay.  I made the script and I think it works.  Now how do I get my 
template to call it?

[Guru]
Oops, somehow you have to pass arguments to the script.  Dang.  Page 
templates don't provide a way to do that without Python expressions. 
You'll need a Python expression after all.  Try this (replace "value" 
with whatever variable you're using):

<span tal:content="python: container.money(container, value)">
$15.00
</span>

[Newbie]
That worked.  But it's going to take a long time to figure out that 
script you sent me.

=======================================


Now imagine this exchange:


=======================================

[Newbie]
I just downloaded Zope today and it's really cool.  I made a little 
checkbook register using a page template.  It's way better than PHP. 
Now I can't figure out how to show only two digits after the decimal 
point.  I don't know Python.  Can you help?

[Guru]
Use the string formatting API.  Try this (replace "value" with whatever 
variable you're using):

   <div tal:namespaces="sf zope:string-fmt">
   <span tal:content="value/sf:money">$15.00</span>
   </div>

[Newbie]
Hey, neat, that makes sense.  But do I have to write "tal:namespaces" 
each time I use "sf:money"?

[Guru]
You can put tal:namespaces at the top of your template.  It applies to 
all contained elements.

[Newbie]
Oh, okay.  I guess that means I could write it this way too:

   <span tal:namespaces="sf zope:string-fmt"
     tal:content="value/sf:money">$15.00</span>

[Guru]
Yep.  I think I'll add a recipe to zopelabs.com.

[Newbie]
Don't bother, this is easy enough.  Thanks!

=======================================


'nuff said.

Shane