<?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>Digital Ambulation &#187; tpl</title>
	<atom:link href="http://www.alanbriolat.co.uk/tag/tpl/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.alanbriolat.co.uk</link>
	<description>Life, programming and general geekery</description>
	<lastBuildDate>Mon, 09 Jan 2012 12:07:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Tpl &#8211; A tiny PHP template engine</title>
		<link>http://www.alanbriolat.co.uk/2009/04/tpl-a-tiny-php-template-engine/</link>
		<comments>http://www.alanbriolat.co.uk/2009/04/tpl-a-tiny-php-template-engine/#comments</comments>
		<pubDate>Thu, 30 Apr 2009 16:03:44 +0000</pubDate>
		<dc:creator>Alan</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tpl]]></category>

		<guid isPermaLink="false">http://www.alanbriolat.co.uk/?p=303</guid>
		<description><![CDATA[While developing CodeScape Framework I came up with a simple template engine that instead of implementing it&#8217;s own language just provided some structure-related hooks, and I&#8217;ve now got around to separating out and releasing Tpl, a simple, self-contained, hierarchical template engine for PHP. Tpl&#8217;s role is centred around template structure, not content&#8212;it&#8217;s not a huge [...]]]></description>
			<content:encoded><![CDATA[<p>While developing <a href="http://codescape.net/csf/">CodeScape Framework</a> I came up with a simple template engine that instead of implementing it&#8217;s own language just provided some structure-related hooks, and I&#8217;ve now got around to separating out and releasing <a href="http://codescape.net/tpl/">Tpl</a>, a simple, self-contained, hierarchical template engine for PHP.</p>
<p>Tpl&#8217;s role is centred around template structure, not content&#8212;it&#8217;s not a huge library of helper functions for creating links, displaying images, etc.  Like any template engine, the aim is to separate presentation logic from application logic, but it doesn&#8217;t attempt this by creating a template language.  PHP is <a href="http://php.net/manual/en/history.php.php">designed</a> to be interleaved with &#8220;static&#8221; output, which means it&#8217;s already a template language.  Trying to replace it for the purpose of templating rarely adds anything useful, incurs hefty performance overheads (which you then have to mitigate with caching) and usually is more restrictive than PHP.  To me, that doesn&#8217;t seem like a sensible application of effort.  Instead Tpl provides its functionality in the form of hooks&#8212;functions which are called from within the template to specify the structure.  Instead of PHP being replaced, it is extended.</p>
<p>The main motivating factor for creating Tpl is that all PHP template engines seem to be centred around <em>inclusion</em> rather than <em>inheritance</em>, something I only noticed after spending time working with <a href="http://www.djangoproject.com/">Django</a>.  For example, a typical PHP template might look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;!-- header.php --&gt;
&lt;html&gt;
    &lt;head&gt;&lt;title&gt;<span style="color: #000000; font-weight: bold;">&lt;?=</span><span style="color: #000088;">$title</span><span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/title&gt;&lt;/head&gt;
    &lt;body&gt;
        &lt;div id=&quot;header&quot;&gt;Website Name&lt;/div&gt;</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;!--</span> footer<span style="color: #339933;">.</span>php <span style="color: #339933;">--&gt;</span>
        <span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;footer&quot;</span><span style="color: #339933;">&gt;</span>Some footer text<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;/</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">&gt;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;!-- content.php --&gt;
<span style="color: #000000; font-weight: bold;">&lt;?</span> <span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'header.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
&lt;div id=&quot;content&quot;&gt;
    Some content
&lt;/div&gt;
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?</span> <span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'footer.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>The main problem with this is that it bears no real relation to the logical structure of the page, just the order in which stuff should appear in the HTML.  If you modify something in the header, you need to make sure you&#8217;ve updated the footer too.  The equivalent Django template would look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">{# base.html #}
&lt;html&gt;
    &lt;head&gt;&lt;title&gt;{{ title }}&lt;/title&gt;&lt;/head&gt;
    &lt;body&gt;
        &lt;div id=&quot;header&quot;&gt;Website Name&lt;/div&gt;
&nbsp;
        {% block content %}{% endblock %}
&nbsp;
        &lt;div id=&quot;footer&quot;&gt;Some footer text&lt;/div&gt;
    &lt;/body&gt;
&lt;/html&gt;</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">{# content.html #}
{% extends &quot;base.html&quot; %}
&nbsp;
{% block content %}
&lt;div id=&quot;content&quot;&gt;
    Some content
&lt;/div&gt;
{% endblock %}</pre></div></div>

<p>The point to note is that the structure is coherent and in one place, and the parts that change are defined as logical blocks which can be replaced.  This is the method I&#8217;ve adopted in Tpl&#8212;the equivalent template would look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;!-- base.php --&gt;
&lt;html&gt;
    &lt;head&gt;&lt;title&gt;<span style="color: #000000; font-weight: bold;">&lt;?=</span><span style="color: #000088;">$C</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'title'</span><span style="color: #009900;">&#93;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/title&gt;&lt;/head&gt;
    &lt;body&gt;
        &lt;div id=&quot;header&quot;&gt;Website Name&lt;/div&gt;
&nbsp;
        <span style="color: #000000; font-weight: bold;">&lt;?</span> Tpl<span style="color: #339933;">::</span><span style="color: #004000;">block</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'content'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span><span style="color: #000000; font-weight: bold;">&lt;?</span> Tpl<span style="color: #339933;">::</span><span style="color: #004000;">endblock</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
        &lt;div id=&quot;footer&quot;&gt;Some footer text&lt;/div&gt;
    &lt;/body&gt;
&lt;/html&gt;</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;!-- content.php --&gt;
<span style="color: #000000; font-weight: bold;">&lt;?</span> Tpl<span style="color: #339933;">::</span><span style="color: #004000;">inherit</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'base.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?</span> Tpl<span style="color: #339933;">::</span><span style="color: #004000;">block</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'content'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;div id=&quot;content&quot;&gt;
    Some content
&lt;/div&gt;
<span style="color: #000000; font-weight: bold;">&lt;?</span> Tpl<span style="color: #339933;">::</span><span style="color: #004000;">endblock</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>More information about Tpl and how to download it can be found at the <a href="http://codescape.net/tpl/">Tpl project page</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.alanbriolat.co.uk/2009/04/tpl-a-tiny-php-template-engine/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

