[ZPT] Truncating strings
Fred L. Drake, Jr.
fred at zope.com
Tue Oct 21 17:55:45 EDT 2003
Ian Bicking writes:
> <span tal:condition="python: len(var) > 10" tal:omit-tag="">
> <span tal:replace="python: var[:10]">var</span>...
> </span>
> <span tal:condition="python: len(var) < 10" tal:omit-tag="">
> <span tal:replace="var">var</span>
> </span>
You can do this with less crud:
--------------------------------------------------------
<tal:block define="var string:abcdefghijklmnop;
maxlen python:10;
toolong python:len(var) > maxlen;
partial python:var[:maxlen]">
<span tal:replace="partial"
/><tal:span condition="toolong">...</tal:span>
</tal:block>
--------------------------------------------------------
What changed:
- I used the tal: prefix on elements where you use tal:omit-tag="";
this is really just a cosmetic difference.
- Except where I used tal:replace; using tal:replace is like using
tal:content and tal:omit-tag="" at the same time.
- I kept all the python: expressions in one place, so it's easier to
keep an eye on what's happening.
- I avoided re-computing any value; everything gets a name, no matter
how short-lived.
- I encoded the > operator since an XML parser would scream about
that, and I'm much more interested in the XML case.
Either approach can certainly be sliced and diced in a variety of
ways.
> Well, that's not very appealing, is it? Or a Python Script:
Your Python Script solution is really only available in Zope 2.
> Really ZPT (TALES really) is painfully in need of easy-to-access
> formatters. I saw discussions of it long ago in the archives, but is
> there any progress on this? E.g.:
Evan has proposed an approach to bundling a handful of code with a
page template; that has some real merit. I don't know what the
implementation status is.
Some work has been done on a branch to make it easier to support
formatting within path expressions, but that hasn't made it to the
trunk yet either.
-Fred
--
Fred L. Drake, Jr. <fred at zope.com>
PythonLabs at Zope Corporation
More information about the ZPT
mailing list