<?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>Jason R Briggs &#187; template</title>
	<atom:link href="http://www.briggs.net.nz/log/tag/template/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.briggs.net.nz/log</link>
	<description>Techie stuff from the perspective of a Kiwi abroad</description>
	<lastBuildDate>Mon, 28 Jun 2010 06:45:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Introducing the Proton Template Engine (part 3)</title>
		<link>http://www.briggs.net.nz/log/2009/01/29/introducing-proton-part-3/</link>
		<comments>http://www.briggs.net.nz/log/2009/01/29/introducing-proton-part-3/#comments</comments>
		<pubDate>Thu, 29 Jan 2009 11:36:27 +0000</pubDate>
		<dc:creator>jrbriggs</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[internet]]></category>
		<category><![CDATA[technical]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[proton]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[template]]></category>
		<category><![CDATA[xhtml]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.briggs.net.nz/log/?p=509</guid>
		<description><![CDATA[Templates are usually made up of other templates, especially if you want to re-use page/document components. Proton supports an include function to embed one template inside another. For example, a fragment of a template (include2.xhtml) containing 2 paragraphs of text&#8230; &#60;div&#62; &#60;p eid="para1"&#62;PARA 1 TEXT&#60;/p&#62; &#60;p eid="para2"&#62;PARA 2 TEXT&#60;/p&#62; &#60;/div&#62; &#8230;can be embedded in another [...]]]></description>
			<content:encoded><![CDATA[<p>Templates are usually made up of other templates, especially if you want to re-use page/document components.  Proton supports an <code>include</code> function to embed one template inside another.</p>
<p>For example, a fragment of a template (include2.xhtml) containing 2 paragraphs of text&#8230;</p>
<pre>
&lt;div&gt;
    &lt;p eid="para1"&gt;PARA 1 TEXT&lt;/p&gt;
    &lt;p eid="para2"&gt;PARA 2 TEXT&lt;/p&gt;
&lt;/div&gt;
</pre>
<p>&#8230;can be embedded in another template (include1.xhtml)&#8230;</p>
<pre>
&lt;body&gt;
    &lt;h1 eid="title"&gt;PAGE TITLE GOES HERE&lt;/h1&gt;

    &lt;div eid="includedcontent"&gt;
    INCLUDED CONTENT GOES HERE
    &lt;/div&gt;
&lt;/body&gt;
</pre>
<p>&#8230;and then populated with data, using the following snippet of code:</p>
<pre><code>
tmp = self.templates['include1.xhtml']
tmp.setelement('title', 'Page Title')
tmp.include('includedcontent', 'include2.xhtml')
tmp.setelement('para1', 'First paragraph of text')
tmp.setelement('para2', 'Second paragraph of text')
</code></pre>
<p>&nbsp;</p>
<p>On the &#8216;syntactic sugar front&#8217;, as an alternative to the more explicit <code>repeat</code> function (for repeating elements in the output), Proton supports setting lists of values rather than single-values. Consider the template:</p>
<pre>
&lt;ul&gt;
    &lt;li rid="list-item" eid="list-item"&gt;LIST ITEMS&lt;/li&gt;
&lt;/ul&gt;
</pre>
<p>The list can be populated without having to explicitly call <code>repeat</code>, using the following code snippet:</p>
<pre><code>
lst = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g' ]
tmp.setelement('list-item', lst)
</code></pre>
<p>If your objects are decorated with properties (@property), then Proton also supports automatically setting the values in the page, using the convention of id followed by colon followed by property.  For example, using the following template:</p>
<pre>
&lt;dl&gt;
    &lt;dt&gt;X Value&lt;/dt&gt;
    &lt;dd eid="prop:x"&gt;prop x here&lt;/dd&gt;
    &lt;dt&gt;Y Value&lt;/dt&gt;
    &lt;dd eid="prop:y"&gt;prop y here&lt;/dd&gt;
&lt;/dl&gt;
</pre>
<p>A class <code>Temp</code>, with properties <code>x</code> and <code>y</code> can be set in the template, with the name <code>prop</code> and the values will be automatically populated.</p>
<pre><code>
class Temp(object):
    def __init__(self, x, y):
        self._x = x
        self._y = y

    @property
    def x(self):
        return self._x

    @property
    def y(self):
        return self._y

t = Temp('100', '500')
tmp.setelement('prop', t)
</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://www.briggs.net.nz/log/2009/01/29/introducing-proton-part-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Introducing the Proton Template Engine (part 2)</title>
		<link>http://www.briggs.net.nz/log/2009/01/27/introducing-proton-part-2/</link>
		<comments>http://www.briggs.net.nz/log/2009/01/27/introducing-proton-part-2/#comments</comments>
		<pubDate>Tue, 27 Jan 2009 11:55:49 +0000</pubDate>
		<dc:creator>jrbriggs</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[internet]]></category>
		<category><![CDATA[technical]]></category>
		<category><![CDATA[proton]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[template]]></category>
		<category><![CDATA[xhtml]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.briggs.net.nz/log/?p=506</guid>
		<description><![CDATA[In a typical template engine, there will be some form of conditional logic, for changing the display of a section of a document. This will generally be an if-statement wrapping one or more elements, perhaps enabling or disabling sections based on access rights, showing more detail based on data, and so on. Proton supports this [...]]]></description>
			<content:encoded><![CDATA[<p>In a typical template engine, there will be some form of conditional logic, for changing the display of a section of a document.  This will generally be an if-statement wrapping one or more elements, perhaps enabling or disabling sections based on access rights, showing more detail based on data, and so on.</p>
<p>Proton supports this logic with the facility to hide an element (marked with the <i>eid</i> attribute).  For example, in this excerpt of a navigation menu:</p>
<pre>
&lt;ul class="menu"&gt;
    &lt;li eid="accounts"&gt;&lt;a href="/accounts"&gt;Account Summary&lt;/a&gt;&lt;/li&gt;
    &lt;li eid="transactions"&gt;&lt;a href="/transactions"&gt;View / Download Transactions&lt;/a&gt;&lt;/li&gt;
    &lt;li eid="transfer"&gt;&lt;a href="/transfer"&gt;Transfer Funds&lt;/a&gt;&lt;/li&gt;
    &lt;li eid="bills"&gt;&lt;a href="/bills"&gt;Bill Payments&lt;/a&gt;&lt;/li&gt;
    &lt;li eid="autopayments"&gt;&lt;a href="/autopayments"&gt;Automatic Payments&lt;/a&gt;&lt;/li&gt;
    &lt;li eid="exchange"&gt;&lt;a href="/exchange"&gt;Foreign Exchange&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</pre>
<p>The following code will switch off the &#8220;transactions&#8221;, &#8220;autopayments&#8221; and &#8220;exchange&#8221; elements:</p>
<pre><code>
tmp.hide('autopayments')
tmp.hide('exchange')
tmp.hide('transactions')
</code></pre>
<p>Resulting in output of:</p>
<pre>
&lt;ul class="menu"&gt;
    &lt;li&gt;&lt;a href="/accounts"&gt;Account Summary&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="/transfer"&gt;Transfer Funds&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="/bills"&gt;Bill Payments&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</pre>
<p>You can see this example in action in the test <a href="http://proton-te.googlecode.com/hg/python/test/hiding.py">hiding.py</a>.</p>
<p>Another, less common but equally useful, feature of template engines is the facility to translate text (in other words, i18n or the internationalisation of output).  Proton supports translation by setting a translation function on a template.  For example, consider the following template:</p>
<pre>
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Page Title&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;h1&gt;Page Title&lt;/h1&gt;

    &lt;p id="para1"&gt;Some translated text&lt;/p&gt;
    &lt;p id="para2"&gt;Not translated text&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>In this case, translation doesn&#8217;t rely on the 3 attributes (eid, aid, rid) &#8212; rather a default set of named (xhtml) elements are translated (the set can, of course, be overridden).  The following code demonstrates translation in action:</p>
<pre><code>
fr = {
    'Page Title' : 'Titre de la page',
    'Some translated text' : 'Certains textes traduits'
}

def translate(text):
    if text in fr:
        return fr[text]
    else:
        return text

tmp = self.templates['tests/i18n.xhtml']
tmp.translate = translate
</code></pre>
<p>And the resulting output:</p>
<pre>
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Titre de la page&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;h1&gt;Titre de la page&lt;/h1&gt;

    &lt;p id="para1"&gt;Certains textes traduits&lt;/p&gt;
    &lt;p id="para2"&gt;Not translated text&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.briggs.net.nz/log/2009/01/27/introducing-proton-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Introducing the Proton Template Engine</title>
		<link>http://www.briggs.net.nz/log/2009/01/20/introducing-proton/</link>
		<comments>http://www.briggs.net.nz/log/2009/01/20/introducing-proton/#comments</comments>
		<pubDate>Tue, 20 Jan 2009 06:27:31 +0000</pubDate>
		<dc:creator>jrbriggs</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[internet]]></category>
		<category><![CDATA[technical]]></category>
		<category><![CDATA[proton]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[template]]></category>
		<category><![CDATA[xhtml]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.briggs.net.nz/log/?p=484</guid>
		<description><![CDATA[I haven&#8217;t come across many (any?) templating engines for Python 3, so it seemed like a good idea to update my own attempt, and then release as an open source (LGPL) project. Proton is a simple, &#8220;code-less&#8221; engine for xml/xhtml templates. Code-less, because it uses 3 types of ID (attribute) in a template file, rather [...]]]></description>
			<content:encoded><![CDATA[<p>I haven&#8217;t come across many (any?) templating engines for <a href="http://docs.python.org/3.0/">Python 3</a>, so it seemed like a good idea to update my own attempt, and then release as an open source (<a href="http://proton-te.googlecode.com/hg/python/proton/license.txt">LGPL</a>) project.</p>
<p><a href="http://code.google.com/p/proton-te">Proton</a> is a simple, &#8220;code-less&#8221; engine for xml/xhtml templates.  Code-less, because it uses 3 types of ID (attribute) in a template file, rather than snippets of code &#8212; this moves the complexity out of the template and into the application (where I think it belongs).  </p>
<p>The following template demonstrates basic use of the 3 IDs:</p>
<pre>
&lt;html&gt;
&lt;head&gt;
&lt;title eid="title"&gt;PAGE TITLE&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;h1 eid="title"&gt;PAGE TITLE&lt;/h1&gt;

    &lt;p&gt;&lt;a eid="link" aid="link" href=""&gt;LINK ITEM&lt;/a&gt;&lt;/p&gt;

    &lt;ul&gt;
        &lt;li rid="list-item" eid="list-item"&gt;LIST ITEMS&lt;/li&gt;
    &lt;/ul&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>The attribute &#8220;<em>eid</em>&#8221; is used to change the value of an element, or hide the element.  &#8220;<em>aid</em>&#8221; is used to change the value of an attribute, and &#8220;<em>rid</em>&#8221; is used for repeating elements.   This might seem a bit simplistic, but it covers the fundamentals of most templating tasks.</p>
<p>The following code demonstrates the use of the template:</p>
<pre><code>
tmp = Templates()['test.xhtml']

tmp.setelement('title', 'An Xhtml Page', '*')
tmp.setelement('link', 'This is a link to Google')
tmp.setattribute('link', 'href', 'http://www.google.com')

tmp.repeat('list-item', 5)
for x in range(0, 5):
    tmp.setelement('list-item', 'test%s' % x, x)

print(str(tmp))
</code></pre>
<p>I used an earlier version of Proton for my own (rather ill-fated) project; a web-based system for timesheet and invoice management, so it has been tested in some relatively complicated scenarios &#8212; however, I am keen to hear where it falls down.  i.e. complex pages where using 3 attributes just doesn&#8217;t cut it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.briggs.net.nz/log/2009/01/20/introducing-proton/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
