[ZPT] Truncating strings

Shane Hathaway shane at zope.com
Thu Oct 23 00:28:52 EDT 2003


On Wed, 22 Oct 2003, Ian Bicking wrote:

> On Wednesday, October 22, 2003, at 09:47 PM, Shane Hathaway wrote:
> > On Tue, 21 Oct 2003, Ian Bicking wrote:
> >> [I think tal:format would imply structure]  But the current access to
> >> these sorts of functions through the modules variable is painful at
> >> best.  And it seems like the functions in PythonScripts.standard are a
> >> little sparse.
> >
> > Once again, this would be beautifully simple with subpath prefixes.
> > Behold:
> >
> > <span tal:content="var/fmt:truncate/10"></span>
> >
> > I keep trying to tell everyone why this is so great.  Does anyone 
> > listen?
> > :-)
> 
> Thinking about the same thing, I'd intuitively come up with:
> 
> <span tal:content="fmt/truncate/10:var"/>
> 
> Which would be equivalent to fmt.truncate['10'](var).  I.e., just use : 
> to mean call.  This is a little problematic because : is already being 
> used elsewhere (not:, python:), but there are a fixed number of such 
> prefixes and they are clearly defined (and can't have a '/' in them).
> 
> When I first saw your expression, I actually tried to parse it in just 
> that way.  Is there something that your proposal can accomplish that 
> simple calling can't?

Here are some things I would consider with your approach:

1. You have to provide the name "fmt" somehow.  Is it a local/global 
variable?  The approach I described doesn't even require an import.

2. You have to rearrange the expression.  In fact, your syntax only works 
if the thing you're formatting is a variable.  How would you format 
"here/var" without storing "here/var" in a temporary variable first?  I 
can write "here/var/fmt:truncate/10".  In fact, I could also write 
"here/var/truncate:10" if I decided to register a "truncate" prefix.

3. There is no precedent for the idea that ":" means you should call 
something.  There is a precedent, however, for the idea that ":" means you 
should switch to a different namespace (XML).

4. You'd be better off writing your expression in plain old Python.  More 
explicit and no new syntax.  But again, the thing you've lost is that you 
have to import something first, and that's a costly thing to do in a 
template.

Shane



More information about the ZPT mailing list