[Zope3-Users] Re: How to make a new namespace for pagetemplates?

Philipp von Weitershausen philipp at weitershausen.de
Fri Sep 8 07:45:45 EDT 2006



FB wrote:
> my employer want to have all external links marked with a small icon telling
> anonymous users from the internet that everything behind given links is
> beyond our responsibility.
> 
> Currenty I have to do something like that:
> 
>    <a tal:define="linkwriter nocall:context/@@linkwriter"
>       tal:replace="structure linkwriter.link(context.url,text=context.urldescription)" />
> 
> ... which doesn't really looks like a nice method to produce i.e. this:
> 
>    <a class="external" title="You're leaving out institute's domain. We're not responsible!"
>       href="http://example.com">This is an example company</a>
> 
> The 'linkwriter' chooses the a-tag's class and title according to the domain of the href.
> I would like to make writing those pagetemplates a bit easier - maybe like that:
> 
>    <a tal:attributes="href context/url" tal:content="context/urldescription"
>       linkwriter:marklink="" />
> 
> How complicated is it to write an additional pagetemplate namespace which is able to
> "postprocess" a tag, after 'tal', 'i18n' and 'metal' did their work?

It would involve hacking Page Template sources beyond recoginition :).

There are several ways to tackle your problem:

* Solution outside of Zope.

Martijn suggested a JavaScript. If you need it to work for 
non-JavaScript browsers, I suggest a WSGI middleware that checks for 
text/html data and transforms <a> elements as needed. You could do the 
transformation in XSLT or Python (e.g. via elementtree).

* Solution within Page Templates:

Write a new TALES expression type (like string: or python:) that 
constructs link tags. This could like:

   <a tal:replace="structure link:context/url context/urldescription">
     link goes here
   </a>

The link: expression would split the argument string, take the first 
part as the URL and the second one as the link description. Both would 
be fed through the PathExpression so that they're evaluated.

Custom TALES expression types are components providing 
zope.tales.interfaces.ITALESExpression. Look at zope.tales.expressions 
for the implementation of the standard TALES expressions.

* Solution outside Page Templates:

Write a view for all objects that you want to generate URLs from. The 
feasibility of this solution depends on whether you always have objects 
with 'url' and 'urldescription' attributes (or similar) to generate 
links from. Then you could write:

   <a tal:replace="structure context/@@html_link">link goes here</a>

The html_link view would generate the whole <a> tag accordingly. If 
you'd like to keep things flexible, the html_link view would first adapt 
the context to something like ILinkInformation that has the 'url' and 
'urldescription' attributes.

HTH

Philipp



More information about the Zope3-users mailing list