<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Joys and rants of a Python programmer &#187; Zope3</title>
	<atom:link href="http://blog.pow.lt/tag/zope3/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.pow.lt</link>
	<description>Pow! Wham, bam, kapow!</description>
	<lastBuildDate>Wed, 02 Dec 2009 16:04:10 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Formatting and processing text in TAL templates</title>
		<link>http://blog.pow.lt/2009/12/02/formatting-and-processing-text-in-tal-templates/</link>
		<comments>http://blog.pow.lt/2009/12/02/formatting-and-processing-text-in-tal-templates/#comments</comments>
		<pubDate>Wed, 02 Dec 2009 15:58:15 +0000</pubDate>
		<dc:creator>Ignas Mikalajūnas</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Zope3]]></category>

		<guid isPermaLink="false">http://blog.pow.lt/?p=92</guid>
		<description><![CDATA[
Marius Gedminas wrote
about a
useful technique for formatting paragraphs &#8212; registering a
view for &#8220;*&#8221; that does the processing for you.



  While developing SchoolTool we
  used a bit more complicated, but slightly more secure technique
  &#8212; adapters implementing IPathAdapter.



  To get the same effect that was described in Marius blog post you do [...]]]></description>
			<content:encoded><![CDATA[<p>
<a href="http://gedmin.as/">Marius Gedminas</a> wrote
about <a href="http://mg.pov.lt/blog/zope3-displaying-multiline-text.html">a
useful technique for formatting paragraphs</a> &mdash; registering a
view for &#8220;*&#8221; that does the processing for you.
</p>

<p>
  While developing <a href="http://schooltool.org">SchoolTool</a> we
  used a bit more complicated, but slightly more secure technique
  &mdash; adapters implementing <code>IPathAdapter</code>.
</p>

<p>
  To get the same effect that was described in Marius blog post you do this:
</p>

<pre class="brush: python;">
import cgi

from zope.interface import implements
from zope.traversing.interfaces import ITraversable
from zope.traversing.interfaces import IPathAdapter
from zope.component import adapts


class TalFiltersPathAdapter(object):
    &quot;&quot;&quot;Collection of filters to be used in views for text processing.&quot;&quot;&quot;

    adapts(None)
    implements(IPathAdapter, ITraversable)

    def __init__(self, context):
        self.context = context

    def traverse(self, name, furtherPath=()):
        handler = getattr(self, name)
        return handler(furtherPath)

    def paragraphs(self, furtherPath=()):
        if self.context is None:
            return ''
        paras = filter(None, [s.strip() for s in self.context.splitlines()])
        return &quot;&quot;.join('&lt;p&gt;%s&lt;/p&gt;\n' % cgi.escape(p)
                        for p in paras)

</pre>

Register it like this:


<pre class="brush: xml;">
   &lt;zope:adapter
       for=&quot;*&quot;
       name=&quot;filter&quot;
       provides=&quot;zope.traversing.interfaces.IPathAdapter&quot;
       factory=&quot;.TalFiltersPathAdapter&quot;
       /&gt;
</pre>

And use it like this:

<pre class="brush: xml; light: true;">
&lt;p tal:replace=&quot;structure object/attribute/filter:paragraphs&quot; /&gt;
</pre>

<p>
The benefits of this technique &#8211; you don&#8217;t have
a <code>zope.Public</code> view registered on all your objects, it
will not &#8220;hog&#8221; &#8216;paragraphs&#8217; view name (view names unless they are
registered for a specific skin are a <em>shared resource</em>) and you
can only access this functionality from TAL templates. The downside
&mdash; it is slightly more complicated and more TAL specific, so if
you are not using TAL templates you can&#8217;t really use this.
</p>
<a href="http://www.addtoany.com/add_to/digg?linkurl=http%3A%2F%2Fblog.pow.lt%2F2009%2F12%2F02%2Fformatting-and-processing-text-in-tal-templates%2F&amp;linkname=Formatting%20and%20processing%20text%20in%20TAL%20templates" title="Digg" rel="nofollow" target="_blank"><img src="http://blog.pow.lt/wp-content/plugins/add-to-any/icons/digg.png" width="16" height="16" alt="Digg"/></a> <a href="http://www.addtoany.com/add_to/reddit?linkurl=http%3A%2F%2Fblog.pow.lt%2F2009%2F12%2F02%2Fformatting-and-processing-text-in-tal-templates%2F&amp;linkname=Formatting%20and%20processing%20text%20in%20TAL%20templates" title="Reddit" rel="nofollow" target="_blank"><img src="http://blog.pow.lt/wp-content/plugins/add-to-any/icons/reddit.png" width="16" height="16" alt="Reddit"/></a> <a href="http://www.addtoany.com/add_to/delicious?linkurl=http%3A%2F%2Fblog.pow.lt%2F2009%2F12%2F02%2Fformatting-and-processing-text-in-tal-templates%2F&amp;linkname=Formatting%20and%20processing%20text%20in%20TAL%20templates" title="Delicious" rel="nofollow" target="_blank"><img src="http://blog.pow.lt/wp-content/plugins/add-to-any/icons/delicious.png" width="16" height="16" alt="Delicious"/></a> <a href="http://www.addtoany.com/add_to/stumbleupon?linkurl=http%3A%2F%2Fblog.pow.lt%2F2009%2F12%2F02%2Fformatting-and-processing-text-in-tal-templates%2F&amp;linkname=Formatting%20and%20processing%20text%20in%20TAL%20templates" title="StumbleUpon" rel="nofollow" target="_blank"><img src="http://blog.pow.lt/wp-content/plugins/add-to-any/icons/stumbleupon.png" width="16" height="16" alt="StumbleUpon"/></a> <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fblog.pow.lt%2F2009%2F12%2F02%2Fformatting-and-processing-text-in-tal-templates%2F&amp;linkname=Formatting%20and%20processing%20text%20in%20TAL%20templates"><img src="http://blog.pow.lt/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://blog.pow.lt/2009/12/02/formatting-and-processing-text-in-tal-templates/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
