<?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>Three of Coins &#187; programming</title>
	<atom:link href="http://www.3ofcoins.net/tag/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.3ofcoins.net</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Thu, 28 Apr 2011 12:44:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Yaclml in pictures, part II: Templating</title>
		<link>http://www.3ofcoins.net/2010/01/21/yaclml-in-pictures-part-ii-templating/</link>
		<comments>http://www.3ofcoins.net/2010/01/21/yaclml-in-pictures-part-ii-templating/#comments</comments>
		<pubDate>Thu, 21 Jan 2010 14:00:47 +0000</pubDate>
		<dc:creator>Maciej</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[bese]]></category>
		<category><![CDATA[common lisp]]></category>
		<category><![CDATA[ediware]]></category>
		<category><![CDATA[html-template]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[yaclml]]></category>

		<guid isPermaLink="false">http://www.3ofcoins.net/?p=94</guid>
		<description><![CDATA[After a short intermission and explanation/excuse, let&#8217;s go on interesting stuff, Yaclml; and if you still didn&#8217;t read the first part, where I wrote about HTML generation and compared Yaclml to CL-WHO, do so now! Continuing first part&#8216;s focus on comparing Yaclml to Ediware, let&#8217;s start by mentioning that CL-WHO doesn&#8217;t do templating at all; [...]]]></description>
			<content:encoded><![CDATA[<p>After a short intermission and explanation/excuse, let&#8217;s go on interesting stuff, <a href="http://common-lisp.net/project/bese/yaclml.html">Yaclml</a>; and if you still didn&#8217;t read <a href="http://www.3ofcoins.net/2009/02/07/yaclml-in-pictures-part-i-html-generation/">the first part</a>, where I wrote about HTML generation and compared Yaclml to <a href="http://www.weitz.de/cl-who/">CL-WHO</a>, do so now!</p>
<p>Continuing <a href="http://www.3ofcoins.net/2009/02/07/yaclml-in-pictures-part-i-html-generation">first part</a>&#8216;s focus on comparing Yaclml to <a href="http://www.weitz.de/">Edi</a>ware, let&#8217;s start by mentioning that CL-WHO doesn&#8217;t do templating at all; Edi Weitz wrote a separate library for this: <a href="http://www.weitz.de/html-template/">HTML-Template</a>.  It provides simple templating support, which turns template of a HTML file (or actually any text file, not limited to HTML) to a closure which, when called, can fill the template with supplied values and output result to a stream.  Simple enough, based on Perl&#8217;s <a href="http://search.cpan.org/dist/HTML-Template/">HTML::Template</a>—which shows in the templating language syntax.  The template directives of HTML-Template are embedded in HTML comments.</p>
<p>Yaclml includes the templating feature, and it chose a different path.  For me, with a tiny bit of <a href="http://www.zope.org/">Zope</a> background, their path is a little nicer: they decided to support a Lisp variant of Zope&#8217;s <a href="http://wiki.zope.org/ZPT/TAL">Template Attribute Language</a>.  TAL is strictly an XHTML and XML templating language, whose directives are special XML tags and attributes, living in a separate XML namespace.  I find this approach much more elegant than magic directives embedded in comments.  This also makes it possible to use XML/XHTML editing tools (such as Emacs&#8217; <a href="http://www.thaiopensource.com/nxml-mode/">nxml-mode</a> and excellent <a href="http://ourcomments.org/Emacs/nXhtml/doc/nxhtml.html">nxhtml-mode</a> built on top of it) to aid in authoring and validation of the code.  Supposedly it also plays fine with visual design tools, such as Adobe Dreamweaver, but I can&#8217;t confirm that, since I don&#8217;t use those.</p>
<p>In this section, I won&#8217;t go on comparing Yaclml to HTML-Template, because I didn&#8217;t use much of the latter, those two are not as similar as CL-WHO and Yaclml&#8217;s HTML generation, and would require non-trivial examples to really see meaningful differences; besides, Yaclml&#8217;s TAL is obviously better, and anyone will see it from TAL alone. ;)  Seriously, HTML-Template&#8217;s documentation is great, it&#8217;s a simple package, and you can compare it for yourself.  These two packages are similar, main difference being TAL&#8217;s focus on XML and XHTML, and syntactic differences are much more of a matter of taste than it is with HTML generation.  However, Yaclml, and especially its TAL support, is underdocumented (there was some <a href="http://trac.common-lisp.net/ucw/wiki/IntroHtmlTemplating">tutorial</a> over at UCW Wiki, but at the moment it&#8217;s inaccessible, and only option to read it is Google&#8217;s cache) and it&#8217;s really hard to figure out how to use it, especially outside of UCW framework; that&#8217;s the hole I&#8217;m trying to fill here.</p>
<p>During writing this article, I&#8217;ve found that Yaclml is actually <em>somewhat</em> documented, in the <a href="http://common-lisp.net/project/bese/qbook">qbook</a> format, but the generated, readable docs are nowhere to be found.  I&#8217;ve rebuilt the HTML files and uploaded them at <a href="http://common-lisp.net/~mpasternacki/yaclml-qbook/">http://common-lisp.net/~mpasternacki/yaclml-qbook/</a>.  These are auto-generated API docs, so it&#8217;s not an easy-to-read tutorial, but rather a reference, and some descriptions in there might be dated; however, it may come in handy when you explore Yaclml&#8217;s APIs on your own.</p>
<h2>Using templates</h2>
<p>Let&#8217;s start from the basic <em>Hello, World</em> template and see how to render it from Lisp. I will write more about the template language in the next section, but I want to write about the Lisp part first, so that you&#8217;re able to run and test more complex examples as you read.  Here&#8217;s the template:</p>
<p><!-- htmlize tmpl1.xml --></p>
<pre style="color: #000000; background-color: #EAF0F0;">
&lt;<span style="color: #00008b;">html</span> <span style="color: #ff6347;">lang</span>=<span style="color: #4d4d4d;">"</span><span style="color: #4d4d4d;">en</span><span style="color: #4d4d4d;">"</span>
      <span style="color: #b03060;">xmlns</span>:<span style="color: #ff6347;">tal</span>=<span style="color: #4d4d4d;">"</span><span style="color: #4d4d4d;">http://common-lisp.net/project/bese/tal/core</span><span style="color: #4d4d4d;">"</span><span style="color: #8b0000; font-weight: bold;">&gt;</span>
  &lt;<span style="color: #00008b;">head</span>&gt;
  &lt;<span style="color: #00008b;">title</span>&gt;Hello, world!&lt;/<span style="color: #00008b;">title</span>&gt;
  &lt;/<span style="color: #00008b;">head</span>&gt;
  &lt;<span style="color: #00008b;">body</span>&gt;
    &lt;<span style="color: #00008b;">h1</span>&gt;Hello, World!&lt;/<span style="color: #00008b;">h1</span>&gt;
    &lt;<span style="color: #00008b;">p</span> <span style="color: #8b0000; font-weight: bold;">tal:content</span>=<span style="color: #4d4d4d;">"</span><span style="color: #4d4d4d;">$param</span><span style="color: #4d4d4d;">"</span>&gt;Parameter will go here.&lt;/<span style="color: #00008b;">p</span>&gt;
  &lt;/<span style="color: #00008b;">body</span>&gt;
&lt;/<span style="color: #00008b;">html</span>&gt;
</pre>
<p><!-- end htmlize --></p>
<p>We can see it&#8217;s an XHTML document, which makes it also proper XML, and uses <a href="http://www.w3.org/TR/REC-xml-names/">XML namespaces</a>.  The namespace <code>tal</code> will refer to the templating language tags and attributes; the <code>tal:content</code> attribute&#8217;s meaning is to replace tag&#8217;s interior with value of a variable.  We&#8217;ll get to this later, now we just want to display this.</p>
<p>To render a template from Lisp, we need cooperation of three parts: the <strong>generator</strong>, the <strong>template</strong> itself, and the <strong>environment</strong>. Generator is an object that finds templates by name, and compiles them to efficient closures.  Yaclml provides a <em>filesystem generator</em>, which finds templates as files in specified directories (‘roots’), but it&#8217;s possible to get templates e.g. from SQL database or from network, by creating a class that would inherit from <code>TAL-GENERATOR</code> or <code>FILE-SYSTEM-GENERATOR</code>. A loaded template is a closure which, when called (with an environment and a generator for finding included templates as arguments) renders the template to <code>*YACLML-OUTPUT*</code>. Environment is mapping from variables used in templates to values.</p>
<p>So, let&#8217;s render our template in the simplest way possible:</p>
<p><!-- htmlize yaclml-test.lisp --></p>
<pre style="color: #000000; background-color: #EAF0F0;">
(<span style="color: #551a8b;">defpackage</span> <span style="color: #458b74;">:yaclml-test</span>
  (<span style="color: #b03060;">:use</span> <span style="color: #b03060;">:common-lisp</span> <span style="color: #b03060;">:yaclml</span>))
(<span style="color: #551a8b;">in-package</span> <span style="color: #b03060;">:yaclml-test</span>)

(<span style="color: #551a8b;">defvar</span> <span style="color: #ff6347;">*generator*</span>
  (make-instance 'file-system-generator
                 <span style="color: #b03060;">:root-directories</span> (list *default-pathname-defaults*))
  <span style="color: #4d4d4d;">"A filesystem-based TAL generator that looks for templates only in
  current directory."</span>)

(<span style="color: #551a8b;">defvar</span> <span style="color: #ff6347;">*environment*</span>
  (tal-env 'param <span style="color: #4d4d4d;">"foo"</span>)
  <span style="color: #4d4d4d;">"Simple environment with a single variable."</span>)

(<span style="color: #551a8b;">defvar</span> <span style="color: #ff6347;">*template*</span>
  (load-tal *generator* <span style="color: #4d4d4d;">"test.tal"</span>)
  <span style="color: #4d4d4d;">"Template read from generator."</span>)

<span style="color: #008b8b;">;;; </span><span style="color: #008b8b;">Render the template:
</span>(funcall *template* *environment* *generator*)
<span style="color: #008b8b;">;;; </span><span style="color: #008b8b;">This yields:
</span><span style="color: #008b8b;">;; </span><span style="color: #008b8b;">&lt;html lang="en"&gt;
</span><span style="color: #008b8b;">;;   </span><span style="color: #008b8b;">&lt;head&gt;
</span><span style="color: #008b8b;">;;   </span><span style="color: #008b8b;">&lt;title&gt;Hello, world!&lt;/title&gt;
</span><span style="color: #008b8b;">;;   </span><span style="color: #008b8b;">&lt;/head&gt;
</span><span style="color: #008b8b;">;;   </span><span style="color: #008b8b;">&lt;body&gt;
</span><span style="color: #008b8b;">;;     </span><span style="color: #008b8b;">&lt;h1&gt;Hello, World!&lt;/h1&gt;
</span><span style="color: #008b8b;">;;     </span><span style="color: #008b8b;">&lt;p&gt;foo&lt;/p&gt;
</span><span style="color: #008b8b;">;;   </span><span style="color: #008b8b;">&lt;/body&gt;
</span><span style="color: #008b8b;">;; </span><span style="color: #008b8b;">&lt;/html&gt;
</span></pre>
<p><!-- end htmlize --></p>
<p>Here, we define a filesystem generator, a simple environment, then we load the template using generator, and we call it. Simple. Result is rendered into stream defined in <code>YACLML:*YACLML-STREAM*</code> variable (default is <code>T</code>, which makes output go to <code>*STANDARD-OUTPUT*</code>; macros <code>(YACLML:WITH-YACLML-STREAM STREAM &amp;BODY BODY)</code> and <code>(YACLML:WITH-YACLML-OUTPUT-TO-STRING &amp;BODY BODY)</code> can be used to redirect the output elegantly).</p>
<p>There is not much more to say about generators: the only generator type actually provided by Yaclml is <code>FILE-SYSTEM-GENERATOR</code>, which accepts a list of pathnames naming directories that will be searched for templates, and an optional initarg <code>:CACHEP</code> which tells whether to cache already parsed templates.</p>
<p>To get the template closure, we use the generator and <code>LOAD-TAL</code> function. Alternatively, we can compile template directly from file or string, using <code>COMPILE-TAL-FILE</code> or <code>COMPILE-TAL-STRING</code>. To render a template, we simply call resulting closure, passing it an environment and a generator (which is used for finding templates included by a template being called) as arguments.</p>
<p>Finally, we get to environments. These are used to fill in templates; they map variable names used in TAL expressions to values. Environments are lists of binding sets; binding set may be a hash table, an association list, a CLOS object (in this case, key would be a slot name), or anything on which a method for <code>FETCH-TAL-VALUE</code> generic is defined. A new environment can be constructed from key-value pairs using <code>TAL-ENV</code> function, from list of binding sets using <code>MAKE-STANDARD-TAL-ENVIRONMENT</code>, or from two existing environments with <code>EXTEND-ENVIRONMENT</code>.  The last of these functions effectively allows to create binding stacks in Lisp code.</p>
<h2>Template syntax</h2>
<p>As I already wrote, template is plain XML (usually XHTML), and whole logic is done by active tags and attributes.  Yaclml maps XML namespaces to Lisp packages (see <code>YACLML:*URI-TO-PACKAGE*</code> variable), so you can easily look up definition of any tag with <a href="http://common-lisp.net/project/slime/">SLIME</a> (or—if you&#8217;re one of <em>those people</em>—using your Lisp vendor&#8217;s IDE).</p>
<p>Only package/namespace which actually contains active tags/attributes provided by Yaclml is <code>:IT.BESE.YACLML.TAL</code>, <abbr title="Also Known As">AKA</abbr> <code>:TAL</code>, attributed to namespace <code>http://common-lisp.net/project/bese/tal/core</code>.</p>
<h3>Tags</h3>
<p>There is only a handful of tags, so let&#8217;s start from them.</p>
<h4><code>tal:tal</code></h4>
<p>This tag is semantically neutral—meaningless, and this is why it&#8217;s useful.  It is used whenever we want to do something with templates, but don&#8217;t want to introduce HTML/XML-level elements.  I usually use it to group together a sequence of tags.</p>
<p>For example: if we <a href="#tal_include">include a sub-template</a>, and the included template consists of more than one tag, we need a top-level tag to be well-formed XML, and to e.g. set XML namespace:</p>
<p><!-- htmlize tmpl2.xml --></p>
<pre style="color: #000000; background-color: #EAF0F0;">
&lt;<span style="color: #b03060;">tal</span>:<span style="color: #00008b;">tal</span> <span style="color: #b03060;">xmlns</span>:<span style="color: #ff6347;">tal</span>=<span style="color: #4d4d4d;">"</span><span style="color: #4d4d4d;">http://common-lisp.net/project/bese/tal/core</span><span style="color: #4d4d4d;">"</span>&gt;
  &lt;<span style="color: #00008b;">h1</span> <span style="color: #b03060;">tal</span>:<span style="color: #ff6347;">content</span>=<span style="color: #4d4d4d;">"</span><span style="color: #4d4d4d;">$title</span><span style="color: #4d4d4d;">"</span>&gt;Tytu&#322;&lt;/<span style="color: #00008b;">h1</span>&gt;
  &lt;<span style="color: #00008b;">div</span> <span style="color: #ff6347;">class</span>=<span style="color: #4d4d4d;">"</span><span style="color: #4d4d4d;">container</span><span style="color: #4d4d4d;">"</span> <span style="color: #b03060;">tal</span>:<span style="color: #ff6347;">content</span>=<span style="color: #4d4d4d;">"</span><span style="color: #4d4d4d;">$body</span><span style="color: #4d4d4d;">"</span> /&gt;
&lt;/<span style="color: #b03060;">tal</span>:<span style="color: #00008b;">tal</span>&gt;
</pre>
<p><!-- end htmlize --></p>
<p>Other example: I need to <a href="#tal_when_unless">conditionalize</a> a sequence of list elements in menu:</p>
<p><!-- htmlize tmpl3.xml --></p>
<pre style="color: #000000; background-color: #EAF0F0;">
&lt;<span style="color: #00008b;">ul</span> <span style="color: #ff6347;">id</span>=<span style="color: #4d4d4d;">"</span><span style="color: #4d4d4d;">menu</span><span style="color: #4d4d4d;">"</span> <span style="color: #b03060;">xmlns</span>:<span style="color: #ff6347;">tal</span>=<span style="color: #4d4d4d;">"</span><span style="color: #4d4d4d;">http://common-lisp.net/project/bese/tal/core</span><span style="color: #4d4d4d;">"</span>&gt;
  &lt;<span style="color: #00008b;">li</span>&gt;&lt;<span style="color: #00008b;">a</span> <span style="color: #ff6347;">href</span>=<span style="color: #4d4d4d;">"</span><span style="color: #4d4d4d;">/</span><span style="color: #4d4d4d;">"</span>&gt;Main page&lt;/<span style="color: #00008b;">a</span>&gt;&lt;/<span style="color: #00008b;">li</span>&gt;
  &lt;<span style="color: #00008b;">li</span>&gt;&lt;<span style="color: #00008b;">a</span> <span style="color: #ff6347;">href</span><span style="color: #8b0000; font-weight: bold;">=</span>/sub"&gt;Subpage&lt;/<span style="color: #00008b;">a</span>&gt;&lt;/<span style="color: #00008b;">li</span>&gt;
  &lt;<span style="color: #b03060;">tal</span>:<span style="color: #00008b;">tal</span> <span style="color: #b03060;">tal</span>:<span style="color: #ff6347;">when</span>=<span style="color: #4d4d4d;">"</span><span style="color: #4d4d4d;">$logged_in</span><span style="color: #4d4d4d;">"</span>&gt;
    &lt;<span style="color: #00008b;">li</span>&gt;&lt;<span style="color: #00008b;">a</span> <span style="color: #ff6347;">href</span>=<span style="color: #4d4d4d;">"</span><span style="color: #4d4d4d;">/profile</span><span style="color: #4d4d4d;">"</span>&gt;My profile&lt;/<span style="color: #00008b;">a</span>&gt;&lt;/<span style="color: #00008b;">li</span>&gt;
    &lt;<span style="color: #00008b;">li</span>&gt;&lt;<span style="color: #00008b;">a</span> <span style="color: #ff6347;">href</span>=<span style="color: #4d4d4d;">"</span><span style="color: #4d4d4d;">/logout</span><span style="color: #4d4d4d;">"</span>&gt;Logout&lt;/<span style="color: #00008b;">a</span>&gt;&lt;/<span style="color: #00008b;">li</span>&gt;
  &lt;/<span style="color: #b03060;">tal</span>:<span style="color: #00008b;">tal</span>&gt;
  &lt;<span style="color: #b03060;">tal</span>:<span style="color: #00008b;">tal</span> <span style="color: #b03060;">tal</span>:<span style="color: #ff6347;">unless</span>=<span style="color: #4d4d4d;">"</span><span style="color: #4d4d4d;">$logged_in</span><span style="color: #4d4d4d;">"</span>&gt;
    &lt;<span style="color: #00008b;">li</span>&gt;&lt;<span style="color: #00008b;">a</span> <span style="color: #ff6347;">href</span>=<span style="color: #4d4d4d;">"</span><span style="color: #4d4d4d;">/sign-up</span><span style="color: #4d4d4d;">"</span>&gt;Sign up&lt;/<span style="color: #00008b;">a</span>&gt;&lt;/<span style="color: #00008b;">li</span>&gt;
    &lt;<span style="color: #00008b;">li</span>&gt;&lt;<span style="color: #00008b;">a</span> <span style="color: #ff6347;">href</span>=<span style="color: #4d4d4d;">"</span><span style="color: #4d4d4d;">/login</span><span style="color: #4d4d4d;">"</span>&gt;Log in&lt;/<span style="color: #00008b;">a</span>&gt;&lt;/<span style="color: #00008b;">li</span>&gt;
  &lt;/<span style="color: #b03060;">tal</span>:<span style="color: #00008b;">tal</span>&gt;
&lt;/<span style="color: #00008b;">ul</span>&gt;
</pre>
<p><!-- end htmlize --><br />
<!-- " to bring Emacs back to norm --></p>
<h4 id="tal_lisp"><code>tal:lisp</code></h4>
<p>This is the tag that <em>you should never, ever use</em>.  Seriously.  It is the root of all evil.  Cause of <strong>mixing <abbr title="Model, View, Controler">MVC</abbr> layers</strong> by introducing logic to templates.  However, it was included by Yaclml authors, so I feel obligated to cover it.  And, when you&#8217;re a programmer and create a structure of templates for further use, or library of widgets, it might have some limited use, and might save some keystrokes.  But rule of thumb, when it comes to using this tag, is <strong>don&#8217;t</strong>.</p>
<p>OK, you&#8217;ve been warned.  Now, here&#8217;s how this tag works:  it simply interprets tag&#8217;s content as a <a href="#expressions">TAL expression</a>, which is actually Lisp code sprinkled with a bit of environment magic.  That&#8217;s it.  I don&#8217;t want to spoil you, so no examples for this tag; figure it out yourself (there is not much there to figure out anyway).</p>
<h4 id="tal_include"><code>tal:include</code></h4>
<p>This tag allows one to dynamically include templates within templates.  It requires either <code>tal:name</code> attribute literally specifying name of included template, or <code>tal:name-expression</code> attribute, which is interpreted as a <a href="#expressions">TAL expression</a>, whose result specifies name of included template:</p>
<p><!-- htmlize tmpl4.xml --></p>
<pre style="color: #000000; background-color: #EAF0F0;">
<span style="color: #008b8b;">&lt;!--</span><span style="color: #008b8b;"> inc0.tal </span><span style="color: #008b8b;">--&gt;</span>
&lt;<span style="color: #b03060;">tal</span>:<span style="color: #00008b;">tal</span> <span style="color: #b03060;">xmlns</span>:<span style="color: #ff6347;">tal</span>=<span style="color: #4d4d4d;">"</span><span style="color: #4d4d4d;">http://common-lisp.net/project/bese/tal/core</span><span style="color: #4d4d4d;">"</span>&gt;
  &lt;<span style="color: #b03060;">tal</span>:<span style="color: #00008b;">include</span> <span style="color: #b03060;">tal</span>:<span style="color: #ff6347;">name</span>=<span style="color: #4d4d4d;">"</span><span style="color: #4d4d4d;">inc1.tal</span><span style="color: #4d4d4d;">"</span> /&gt;
  &lt;<span style="color: #00008b;">strong</span> <span style="color: #b03060;">tal</span>:<span style="color: #ff6347;">content</span>=<span style="color: #4d4d4d;">"</span><span style="color: #4d4d4d;">$included</span><span style="color: #4d4d4d;">"</span> /&gt;
  &lt;<span style="color: #b03060;">tal</span>:<span style="color: #00008b;">include</span> <span style="color: #b03060;">tal</span>:<span style="color: #ff6347;">name-expression</span>=<span style="color: #4d4d4d;">"</span><span style="color: #4d4d4d;">${$included}.tal</span><span style="color: #4d4d4d;">"</span> /&gt;
&lt;/<span style="color: #b03060;">tal</span>:<span style="color: #00008b;">tal</span>&gt;
</pre>
<p><!-- end htmlize --></p>
<p><!-- htmlize tmpl4.1.xml --></p>
<pre style="color: #000000; background-color: #EAF0F0;">
<span style="color: #008b8b;">&lt;!--</span><span style="color: #008b8b;"> inc1.tal </span><span style="color: #008b8b;">--&gt;</span>
&lt;<span style="color: #b03060;">tal</span>:<span style="color: #00008b;">tal</span> <span style="color: #b03060;">xmlns</span>:<span style="color: #ff6347;">tal</span>=<span style="color: #4d4d4d;">"</span><span style="color: #4d4d4d;">http://common-lisp.net/project/bese/tal/core</span><span style="color: #4d4d4d;">"</span>&gt;FOO&lt;/<span style="color: #b03060;">tal</span>:<span style="color: #00008b;">tal</span>&gt;
</pre>
<p><!-- end htmlize --></p>
<p>Let&#8217;s render these two templates:</p>
<p><!-- htmlize tmpl4.lisp --></p>
<pre style="color: #000000; background-color: #EAF0F0;">
(funcall (load-tal *generator* <span style="color: #4d4d4d;">"inc0.tal"</span>)
         (tal-env 'included <span style="color: #4d4d4d;">"inc1"</span>)
         *generator*)
<span style="color: #008b8b;">;;; </span><span style="color: #008b8b;">Yields:
</span><span style="color: #008b8b;">;;</span><span style="color: #008b8b;">
</span><span style="color: #008b8b;">;;  </span><span style="color: #008b8b;">FOO
</span><span style="color: #008b8b;">;;  </span><span style="color: #008b8b;">&lt;strong&gt;inc1&lt;/strong&gt;
</span><span style="color: #008b8b;">;;  </span><span style="color: #008b8b;">
</span><span style="color: #008b8b;">;;  </span><span style="color: #008b8b;">FOO
</span><span style="color: #008b8b;">;;</span><span style="color: #008b8b;">
</span></pre>
<p><!-- end htmlize --></p>
<p>But hey!  There&#8217;s more!</p>
<p>I didn&#8217;t yet mention one special XML namespace, <code>xmlns:param=&quot;http://common-lisp.net/project/bese/tal/params&quot;</code>.  It allows you to <em>pass arguments</em> (environment parameters) to included template, which effectively gives you not only parametrized subtemplates, but also <a href="http://docs.djangoproject.com/en/dev/topics/templates/#template-inheritance">template inheritance</a>, a <strong>very</strong> powerful tool for organizing your templates.  Let&#8217;s look into Yaclml&#8217;s own test suite and see how it works:</p>
<p><!-- htmlize tmpl5.xml --></p>
<pre style="color: #000000; background-color: #EAF0F0;">
<span style="color: #008b8b;">&lt;!--</span><span style="color: #008b8b;"> inc3.tal </span><span style="color: #008b8b;">--&gt;</span>
&lt;<span style="color: #b03060;">tal</span>:<span style="color: #00008b;">include</span> <span style="color: #b03060;">xmlns</span>:<span style="color: #ff6347;">tal</span>=<span style="color: #4d4d4d;">"</span><span style="color: #4d4d4d;">http://common-lisp.net/project/bese/tal/core</span><span style="color: #4d4d4d;">"</span>
             <span style="color: #b03060;">xmlns</span>:<span style="color: #ff6347;">param</span>=<span style="color: #4d4d4d;">"</span><span style="color: #4d4d4d;">http://common-lisp.net/project/bese/tal/params</span><span style="color: #4d4d4d;">"</span>
             <span style="color: #b03060;">tal</span>:<span style="color: #ff6347;">name</span>=<span style="color: #4d4d4d;">"</span><span style="color: #4d4d4d;">inc4.tal</span><span style="color: #4d4d4d;">"</span>
             <span style="color: #b03060;">param</span>:<span style="color: #ff6347;">baz</span>=<span style="color: #4d4d4d;">"</span><span style="color: #4d4d4d;">$foo</span><span style="color: #4d4d4d;">"</span>&gt;
  &lt;<span style="color: #b03060;">param</span>:<span style="color: #00008b;">bar</span>&gt;&lt;<span style="color: #b03060;">tal</span>:<span style="color: #00008b;">include</span> <span style="color: #b03060;">tal</span>:<span style="color: #ff6347;">name</span>=<span style="color: #4d4d4d;">"</span><span style="color: #4d4d4d;">inc5.tal</span><span style="color: #4d4d4d;">"</span>/&gt;&lt;/<span style="color: #b03060;">param</span>:<span style="color: #00008b;">bar</span>&gt;
  &lt;<span style="color: #b03060;">param</span>:<span style="color: #00008b;">quux</span>&gt;&lt;<span style="color: #00008b;">p</span>&gt;xyzzy&lt;/<span style="color: #00008b;">p</span>&gt;&lt;/<span style="color: #b03060;">param</span>:<span style="color: #00008b;">quux</span>&gt;
&lt;/<span style="color: #b03060;">tal</span>:<span style="color: #00008b;">include</span>&gt;
</pre>
<p><!-- end htmlize --></p>
<p><!-- htmlize tmpl5.1.xml --></p>
<pre style="color: #000000; background-color: #EAF0F0;">
<span style="color: #008b8b;">&lt;!--</span><span style="color: #008b8b;"> inc4.tal </span><span style="color: #008b8b;">--&gt;</span>
&lt;<span style="color: #b03060;">tal</span>:<span style="color: #00008b;">tal</span> <span style="color: #b03060;">xmlns</span>:<span style="color: #ff6347;">tal</span>=<span style="color: #4d4d4d;">"</span><span style="color: #4d4d4d;">http://common-lisp.net/project/bese/tal/core</span><span style="color: #4d4d4d;">"</span>&gt;
  foo is &lt;<span style="color: #b03060;">tal</span>:<span style="color: #00008b;">tal</span> <span style="color: #b03060;">tal</span>:<span style="color: #ff6347;">replace</span>=<span style="color: #4d4d4d;">"</span><span style="color: #4d4d4d;">$foo</span><span style="color: #4d4d4d;">"</span>&gt;bad bad bad&lt;/<span style="color: #b03060;">tal</span>:<span style="color: #00008b;">tal</span>&gt;,
  bar is &lt;<span style="color: #b03060;">tal</span>:<span style="color: #00008b;">tal</span> <span style="color: #b03060;">tal</span>:<span style="color: #ff6347;">replace</span>=<span style="color: #4d4d4d;">"</span><span style="color: #4d4d4d;">$bar</span><span style="color: #4d4d4d;">"</span>/&gt;,
  baz is &lt;<span style="color: #b03060;">tal</span>:<span style="color: #00008b;">tal</span> <span style="color: #b03060;">tal</span>:<span style="color: #ff6347;">replace</span>=<span style="color: #4d4d4d;">"</span><span style="color: #4d4d4d;">$baz</span><span style="color: #4d4d4d;">"</span> /&gt;, and
  quux is &lt;<span style="color: #b03060;">tal</span>:<span style="color: #00008b;">tal</span> <span style="color: #b03060;">tal</span>:<span style="color: #ff6347;">content-as-is</span>=<span style="color: #4d4d4d;">"</span><span style="color: #4d4d4d;">$quux</span><span style="color: #4d4d4d;">"</span> /&gt;.
&lt;/<span style="color: #b03060;">tal</span>:<span style="color: #00008b;">tal</span>&gt;
</pre>
<p><!-- end htmlize --></p>
<p><!-- htmlize tmpl5.2.xml --></p>
<pre style="color: #000000; background-color: #EAF0F0;">
<span style="color: #008b8b;">&lt;!--</span><span style="color: #008b8b;"> inc5.tal </span><span style="color: #008b8b;">--&gt;</span>
&lt;<span style="color: #b03060;">tal</span>:<span style="color: #00008b;">tal</span> <span style="color: #b03060;">xmlns</span>:<span style="color: #ff6347;">tal</span>=<span style="color: #4d4d4d;">"</span><span style="color: #4d4d4d;">http://common-lisp.net/project/bese/tal/core</span><span style="color: #4d4d4d;">"</span>&gt;BARINCLUDED&lt;/<span style="color: #b03060;">tal</span>:<span style="color: #00008b;">tal</span>&gt;
</pre>
<p><!-- end htmlize --></p>
<p><!-- htmlize tmpl5.lisp --></p>
<pre style="color: #000000; background-color: #EAF0F0;">
(funcall (load-tal *generator* <span style="color: #4d4d4d;">"inc3.tal"</span>)
         (tal-env 'foo <span style="color: #4d4d4d;">"FOOPARAM"</span>)
         *generator*)
<span style="color: #008b8b;">;;; </span><span style="color: #008b8b;">Yields:
</span><span style="color: #008b8b;">;;  </span><span style="color: #008b8b;">foo is FOOPARAM,
</span><span style="color: #008b8b;">;;  </span><span style="color: #008b8b;">bar is BARINCLUDED,
</span><span style="color: #008b8b;">;;  </span><span style="color: #008b8b;">baz is FOOPARAM, and
</span><span style="color: #008b8b;">;;  </span><span style="color: #008b8b;">quux is &lt;p&gt;xyzzy&lt;/p&gt;.
</span></pre>
<p><!-- end htmlize --></p>
<p>We can pass to subtemplates not only plain parameters, we can pass <em>whole HTML subtrees</em>.</p>
<h3>Attributes</h3>
<p>More interesting work, and actual logic, lives in TAL&#8217;s attributes.  Let&#8217;s look at those.</p>
<h4 id="tal_content"><code>tal:content</code>, <code>tal:content-as-is</code>, and <code>tal:replace</code></h4>
<p>These attributes insert into their tag (<code>content</code>) or replace their tag entirely with (<code>replace</code>) with a <a href="#expressions">TAL expression</a>.  Plain <code>content</code> and <code>replace</code> escape HTML special chars (&lt;&quot;&gt;); <code>content-as-is</code> does not escape anything.  We&#8217;ve already seen those in action.</p>
<h4 id="tal_when_unless"><code>tal:when</code> and <code>tal:unless</code></h4>
<p>These tags are <em>conditionals</em>.  They render the tag they belong to (and its content, of course) when a <a href="#expressions">TAL expression</a> is true (<code>tal:when</code>) or false (<code>tal:unless</code>).  We&#8217;ve seen those too.  Unfortunately, there is no if-then-else construct; this would be hard to encode <em>elegantly</em> in XML.</p>
<h4 id="tal_dolist"><code>tal:dolist</code></h4>
<p>Looping construct.  Its <a href="#expressions">TAL expression</a> should return a <em>list of environments</em>.  Its tag will be executed with current environment extended by each of environments on the list.  Let&#8217;s see it:</p>
<p><!-- htmlize tmpl6.xml --></p>
<pre style="color: #000000; background-color: #EAF0F0;">
<span style="color: #008b8b;">&lt;!--</span><span style="color: #008b8b;"> dolist.tal </span><span style="color: #008b8b;">--&gt;</span>
&lt;<span style="color: #00008b;">ul</span> <span style="color: #b03060;">xmlns</span>:<span style="color: #ff6347;">tal</span>=<span style="color: #4d4d4d;">"</span><span style="color: #4d4d4d;">http://common-lisp.net/project/bese/tal/core</span><span style="color: #4d4d4d;">"</span>&gt;
  &lt;<span style="color: #00008b;">li</span> <span style="color: #b03060;">tal</span>:<span style="color: #ff6347;">dolist</span>=<span style="color: #4d4d4d;">"</span><span style="color: #4d4d4d;">$envlist</span><span style="color: #4d4d4d;">"</span>&gt;
    &lt;<span style="color: #00008b;">span</span> <span style="color: #b03060;">tal</span>:<span style="color: #ff6347;">replace</span>=<span style="color: #4d4d4d;">"</span><span style="color: #4d4d4d;">$foo</span><span style="color: #4d4d4d;">"</span> /&gt;
    &lt;<span style="color: #00008b;">em</span> <span style="color: #b03060;">tal</span>:<span style="color: #ff6347;">content</span>=<span style="color: #4d4d4d;">"</span><span style="color: #4d4d4d;">$bar</span><span style="color: #4d4d4d;">"</span>&gt;default&lt;/<span style="color: #00008b;">em</span>&gt;
  &lt;/<span style="color: #00008b;">li</span>&gt;
&lt;/<span style="color: #00008b;">ul</span>&gt;
</pre>
<p><!-- end htmlize --></p>
<p><!-- htmlize tmpl6.lisp --></p>
<pre style="color: #000000; background-color: #EAF0F0;">
(funcall (load-tal *generator* <span style="color: #4d4d4d;">"dolist.tal"</span>)
         (tal-env 'envlist (list (tal-env 'foo 1 'bar 2)
                                 (tal-env 'foo 3 'bar 4)
                                 (tal-env 'foo 5)))
         *generator*)
<span style="color: #008b8b;">;;; </span><span style="color: #008b8b;">Yields:
</span><span style="color: #008b8b;">;; </span><span style="color: #008b8b;">&lt;ul&gt;
</span><span style="color: #008b8b;">;;   </span><span style="color: #008b8b;">&lt;li&gt;
</span><span style="color: #008b8b;">;;     </span><span style="color: #008b8b;">1
</span><span style="color: #008b8b;">;;     </span><span style="color: #008b8b;">&lt;em&gt;2&lt;/em&gt;
</span><span style="color: #008b8b;">;;   </span><span style="color: #008b8b;">&lt;/li&gt;&lt;li&gt;
</span><span style="color: #008b8b;">;;     </span><span style="color: #008b8b;">3
</span><span style="color: #008b8b;">;;     </span><span style="color: #008b8b;">&lt;em&gt;4&lt;/em&gt;
</span><span style="color: #008b8b;">;;   </span><span style="color: #008b8b;">&lt;/li&gt;&lt;li&gt;
</span><span style="color: #008b8b;">;;     </span><span style="color: #008b8b;">5
</span><span style="color: #008b8b;">;;     </span><span style="color: #008b8b;">&lt;em&gt;&lt;/em&gt;
</span><span style="color: #008b8b;">;;   </span><span style="color: #008b8b;">&lt;/li&gt;
</span><span style="color: #008b8b;">;; </span><span style="color: #008b8b;">&lt;/ul&gt;
</span></pre>
<p><!-- end htmlize --></p>
<p>Not most readable or elegant, but expressive and gets the work done.  A bit like next attribute…</p>
<h4 id="tal_let"><code>tal:let</code></h4>
<p>This one, added to Yaclml by yours truly (of which yours truly should be slightly ashamed), extends environment for tag content with variables specified as for <code>LET</code> command.  This breaks layer separation almost as badly as <code>TAL:LISP</code>, could be implemented way better (e.g. by using <code>xmlns:param=&quot;http://common-lisp.net/project/bese/tal/params&quot;</code> namespace and allowing user to use it in all tags, not only <code>tal:include</code>), and is generally evil.  Nevertheless, it&#8217;s already commited to Yaclml, and should be documented.  Please, don&#8217;t look at the following example:</p>
<p><!-- htmlize tmpl7.xml --></p>
<pre style="color: #000000; background-color: #EAF0F0;">
&lt;<span style="color: #00008b;">div</span> <span style="color: #b03060;">xmlns</span>:<span style="color: #ff6347;">tal</span>=<span style="color: #4d4d4d;">"</span><span style="color: #4d4d4d;">http://common-lisp.net/project/bese/tal/core</span><span style="color: #4d4d4d;">"</span>
     <span style="color: #b03060;">tal</span>:<span style="color: #ff6347;">let</span>=<span style="color: #4d4d4d;">"</span><span style="color: #4d4d4d;">((foo 1) (bar 2) (baz 3))</span><span style="color: #4d4d4d;">"</span>&gt;
  &lt;<span style="color: #00008b;">div</span> <span style="color: #b03060;">tal</span>:<span style="color: #ff6347;">replace</span>=<span style="color: #4d4d4d;">"</span><span style="color: #4d4d4d;">$foo</span><span style="color: #4d4d4d;">"</span>/&gt; &lt;<span style="color: #00008b;">div</span> <span style="color: #b03060;">tal</span>:<span style="color: #ff6347;">replace</span>=<span style="color: #4d4d4d;">"</span><span style="color: #4d4d4d;">$bar</span><span style="color: #4d4d4d;">"</span>/&gt; &lt;<span style="color: #00008b;">div</span> <span style="color: #b03060;">tal</span>:<span style="color: #ff6347;">replace</span>=<span style="color: #4d4d4d;">"</span><span style="color: #4d4d4d;">$baz</span><span style="color: #4d4d4d;">"</span>/&gt;
&lt;/<span style="color: #00008b;">div</span>&gt;
</pre>
<p><!-- end htmlize --></p>
<p><!-- htmlize tmpl7.lisp --></p>
<pre style="color: #000000; background-color: #EAF0F0;">
(funcall (load-tal *generator* <span style="color: #4d4d4d;">"let.tal"</span>)
         nil
         *generator**)
<span style="color: #008b8b;">;;; </span><span style="color: #008b8b;">Yields:
</span><span style="color: #008b8b;">;; </span><span style="color: #008b8b;">&lt;div&gt;
</span><span style="color: #008b8b;">;;  </span><span style="color: #008b8b;">1 2 3
</span><span style="color: #008b8b;">;;</span><span style="color: #008b8b;">&lt;/div&gt;
</span></pre>
<p><!-- end htmlize --></p>
<h4 id="tal_in_package"><code>tal:in-package</code></h4>
<p>This attribute sets current package for <a href="#expressions">TAL expressions</a> within its tag.  We&#8217;ll talk about expressions in a moment, now—a silly example:</p>
<p><!-- htmlize tmpl8.xml --></p>
<pre style="color: #000000; background-color: #EAF0F0;">
&lt;<span style="color: #00008b;">div</span> <span style="color: #b03060;">xmlns</span>:<span style="color: #ff6347;">tal</span>=<span style="color: #4d4d4d;">"</span><span style="color: #4d4d4d;">http://common-lisp.net/project/bese/tal/core</span><span style="color: #4d4d4d;">"</span>
     <span style="color: #b03060;">tal</span>:<span style="color: #ff6347;">in-package</span>=<span style="color: #4d4d4d;">"</span><span style="color: #4d4d4d;">:yaclml</span><span style="color: #4d4d4d;">"</span>&gt;
  &lt;<span style="color: #00008b;">pre</span> <span style="color: #b03060;">tal</span>:<span style="color: #ff6347;">content</span>=<span style="color: #4d4d4d;">"</span><span style="color: #4d4d4d;">*uri-to-package*</span><span style="color: #4d4d4d;">"</span>/&gt;
&lt;/<span style="color: #00008b;">div</span>&gt;
</pre>
<p><!-- end htmlize --></p>
<p><!-- htmlize tmpl8.lisp --></p>
<pre style="color: #000000; background-color: #EAF0F0;">
(funcall (load-tal *generator* <span style="color: #4d4d4d;">"inpackage.tal"</span>) nil *generator*)
<span style="color: #008b8b;">;;; </span><span style="color: #008b8b;">Yields:
</span><span style="color: #008b8b;">;; </span><span style="color: #008b8b;">&lt;div&gt;
</span><span style="color: #008b8b;">;;   </span><span style="color: #008b8b;">&lt;pre&gt;(http://common-lisp.net/project/bese/tal/core . #&amp;lt;PACKAGE &amp;quot;IT.BESE.YACLML.TAL&amp;quot;&amp;gt;) (http://common-lisp.net/project/bese/tal/params
</span><span style="color: #008b8b;">;;  </span><span style="color: #008b8b;">. #&amp;lt;PACKAGE &amp;quot;IT.BESE.YACLML.TAL.INCLUDE-PARAMS&amp;quot;&amp;gt;) (http://common-lisp.net/project/bese/yaclml/core
</span><span style="color: #008b8b;">;;  </span><span style="color: #008b8b;">. #&amp;lt;PACKAGE &amp;quot;IT.BESE.YACLML.TAGS&amp;quot;&amp;gt;) (http://www.w3.org/XML/1998/namespace . #&amp;lt;PACKAGE &amp;quot;IT.BESE.YACLML.XML&amp;quot;&amp;gt;) (http://www.w3.org/1999/xlink/ . #&amp;lt;PACKAGE &amp;quot;IT.BESE.YACLML.XLINK&amp;quot;&amp;gt;)&lt;/pre&gt;
</span><span style="color: #008b8b;">;; </span><span style="color: #008b8b;">&lt;/div&gt;
</span></pre>
<p><!-- end htmlize --></p>
<h3 id="expressions">Expressions</h3>
<p>Yaclml&#8217;s TAL expressions are <em>simply Lisp expressions</em>, with all their upsides and downsides.  Current package is one set with <a href="#tal_in_package"><code>tal:in-package</code></a> or, when none was set, <em>package that was active when template was called</em>.  There is one difference, though: readtable is modified.  Symbols following the <code>$</code> prefix are looked up in current environment.  That&#8217;s all.  You can (and should <em>not</em>) use all the power of Lisp in the expressions; remember to quote the double quotation marks (the &quot; sign) as <code>&amp;quot;</code> entities.  Yes, this gets <em>unreadable</em> and <em>ugly</em>.  Simply, don&#8217;t overuse it.  When in doubt, move logic to Lisp code.</p>
<p>These rules apply for most Yaclml attributes; unfortunately, not for all of them.  The ugly exception is the <code>tal:name-expression</code> attribute of the <a href="#tal_include"><code>tal:include</code></a> tag.  This tag, and all plain HTML tags, <strong>can</strong> include TAL expressions, by surrounding it with <code>${<i>tal-expression</i>}</code>.  That&#8217;s where the ugly <code>${$included}.tal</code> syntax in <code>tal:include</code> example came from.</p>
<p>The second form, <code>@{<i>tal-expression</i>}</code>, expects <code><i>tal-expression</i></code> to return a <em>list</em>, and resulting string is concatenation of this list&#8217;s elements.</p>
<h2>Caveats</h2>
<p>Yaclml has some issues to be aware of.  Here are those that I know; this is probably not an exhaustive list, but it should be useful anyway.  So…</p>
<p>Input TAL templates are read and interpreted as XML files.  This means that XML comments are discarded <em>before the interpreter even sees them</em>.  This usually is a good thing; however, if someone tries to use <a href="http://www.quirksmode.org/css/condcom.html">conditional comments</a>, there&#8217;s a nasty surprise: conditionals are eaten by template engine.  In the next part, which will be about extending Yaclml, we&#8217;ll learn, how to work around this.</p>
<p>Attributes aren&#8217;t interpreted consistently: most TAL attributes accept TAL expressions, except the <code>tal:name-expression</code> attribute of the <a href="#tal_include"><code>tal:include</code></a> tag, which is interpreted as plain HTML attribute and needs escaping expressions with <code>${…}</code> syntax; and <code>tal:name</code> attribute of the same tag ignores every attempt to use any syntax at all.</p>
<p>TAL expressions need to be valid XML attributes, so Lisp has to be quoted.  This is especially annoying when you try to use string literals within expressions. However, this has a simple workaround: don&#8217;t pack logic into your expressions.  If this is annoying, make sure you&#8217;re not cramming controller logic into your view layer inside templates.</p>
<h2>Summary</h2>
<p>TAL is not perfect, but it is a solid, usable and quite optimized code base.  For generating HTML and XML documents, it is way more convenient (for me) than text based approaches, as it enforces well-formedness and (to some extent) validity of generated HTML.  It is also extensible, about which I&#8217;ll write in the next part—stay tuned!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.3ofcoins.net/2010/01/21/yaclml-in-pictures-part-ii-templating/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

