Hi, I want to add custom namespaces and/or entities to ZPT. How can I do this? I tried searching the web, but did not find a sollution. What I want to do is something like this: <my-ns:email name="Egon Meier" /> or &author; # link to author's mail-address I know, this is possible using TAL/METAL and tags like <span metal:replace="python:my_ns.email(name='Egon Meier')" /> but this is obvious a bit longish and clumsy to type. Any hints for solving this? +++hartmut
Hartmut Goebel wrote:
I want to add custom namespaces and/or entities to ZPT. How can I do this? I tried searching the web, but did not find a sollution.
What I want to do is something like this:
<my-ns:email name="Egon Meier" /> or &author; # link to author's mail-address
Interesting. ZPT's implementation doesn't have any way of supporting this, although one or more of the alternate TAL implementations may be more easily extensible. You're essentially trying to make your own mini-language here, and while I won't dispute that this would be useful for you, a generalized version of this capability would take a lot of thought. Your best bet may well be a quick&dirty hack along the lines of inserting regex-based search and replace on your templates before they're compiled. Something along the lines of (WARNING: UNTESTED!): txt = re.sub(r'<my-ns:([A-Za-z]+) ([^>]*)>', r'<tal:my-ns replace="python:ms-ns.\1(\2)" />', txt) txt = re.sub(r'&my-([A-Za-z]]+);', r'<tal:my-ent replace="\1" />', txt) You could patch or monkey-patch a call to your transformation into PageTemplate.py's _cook() method. Cheers, Evan @ 4-am
Hi Evan, Evan Simpson schrieb:
Hartmut Goebel wrote:
Interesting. ZPT's implementation doesn't have any way of supporting this, although one or more of the alternate TAL implementations may be more easily extensible.
This is somehow disapointing, since I always tought Zope (and ZPT as being a part of it) is written to be very extensible. Can you pint me to some of the alternate ZPT implementations?
You're essentially trying to make your own mini-language here, and while
No :-) I just want to insert new namespaces beside 'metal:', 'tal:' and 'i18l:. This is not another mini-language, but just using XML to extent the capabilities of ZPT.
I won't dispute that this would be useful for you, a generalized version of this capability would take a lot of thought. Your best bet may well be a quick&dirty hack along the lines of inserting regex-based search and replace on your templates before they're compiled. Something along the lines of (WARNING: UNTESTED!):
Thanks for the snippet. This may be a good workaround, but this is not what I wanted. It's inefficient to regex-replace the string, if it gets parsed just after this. While this may be practical for a few entities or tags, it get both slow, inefficient and unmaintainable when implementing some 10 or even some 100 of tags this way. +++hartmut
Hartmut Goebel wrote:
This is somehow disapointing, since I always tought Zope (and ZPT as being a part of it) is written to be very extensible.
For the most part, yes, but additional namespaces didn't occur to us when implementing ZPT, and it is hardly trivial to define how such extensibility should work.
Can you pint me to some of the alternate ZPT implementations?
All of the implementations that I know of are listed at http://dev.zope.org/Wikis/DevSite/Projects/ZPT, but most of them are not integrated with Zope. Cheers, Evan @ 4-am
Evan Simpson schrieb:
For the most part, yes, but additional namespaces didn't occur to us when implementing ZPT, and it is hardly trivial to define how such extensibility should work.
After having a look into TALInterpreter.py I think a have a clue, why this is complicated. *sign* Otherwise: TALInterpreter uses bytecode_handlers, thus adding new bytecodes should be quite easy. So the only problem being left (as far as I can see) is: how to make adding bytecodes or namespaces extensible. Meanwhile I tried something different: I patched HTMLTALParser.py and ZopePageTemplate/__init__.py to recognice namespaces. One may pass a dict of namespaces to PageTemplate(). If a tag's prefix belongs to a namespace listed here, the tag get patched into somthing like <metal:block tal:define="year string:'1999'" tal:content="python:namspaces['hg'].copyright(year=year)" /> Quite a hack, but works :-) Do you want to get the patch? Never the less I would prefere a really expantable solution.
but most of them are not integrated with Zope.
It seams as if I have bad luck on this topic. Regards +++hartmut -- | Hartmut Goebel | We build the crazy compilers | | h.goebel@crazy-compilers.com | Compiler Manufacturer |
participants (3)
-
Evan Simpson -
Hartmut Goebel -
Hartmut Goebel