<?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>Laughing Meme &#187; smarty</title>
	<atom:link href="http://laughingmeme.org/tag/smarty/feed/" rel="self" type="application/rss+xml" />
	<link>http://laughingmeme.org</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Sun, 29 Jan 2012 21:54:39 +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>Introducing Liquid:  Smarty for Rails?</title>
		<link>http://laughingmeme.org/2005/11/05/introducing-liquid-smarty-for-rails/</link>
		<comments>http://laughingmeme.org/2005/11/05/introducing-liquid-smarty-for-rails/#comments</comments>
		<pubDate>Sun, 06 Nov 2005 05:49:00 +0000</pubDate>
		<dc:creator>Kellan</dc:creator>
				<category><![CDATA[Aside]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[smarty]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://lm.quxx.info/?p=3087</guid>
		<description><![CDATA[I&#8217;m caught between running screaming from the room, or edging out quietly and hoping nobody notices. (Actually I&#8217;m giving Tobi the benefit of the doubt, but the idea sends shivers up my spine)]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m caught between running screaming from the room, or edging out quietly and hoping nobody notices.  (Actually I&#8217;m giving Tobi the benefit of the doubt, but the idea sends shivers up my spine)</p>
<p><a href='http://blog.leetsoft.com/articles/2005/11/05/introducing-liquid'>http://blog.leetsoft.com/articles/2005/11/05/introducing-liquid</a></p>]]></content:encoded>
			<wfw:commentRss>http://laughingmeme.org/2005/11/05/introducing-liquid-smarty-for-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple Object Access from Smarty</title>
		<link>http://laughingmeme.org/2004/08/05/simple-object-access-from-smarty/</link>
		<comments>http://laughingmeme.org/2004/08/05/simple-object-access-from-smarty/#comments</comments>
		<pubDate>Fri, 06 Aug 2004 07:51:14 +0000</pubDate>
		<dc:creator>Kellan</dc:creator>
				<category><![CDATA[oop]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[php5]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[smarty]]></category>

		<guid isPermaLink="false">http://lm.quxx.info/?p=876</guid>
		<description><![CDATA[On one hand it&#8217;s kind of disheartening how much resistance and fear there is towards objects from segments of the PHP community. (hilighted by the recent hostility towards PHP5&#8242;s adding of something resembling a working object model) On the other hand can you blame them when popular tools like Smarty punish you for using object? [...]]]></description>
			<content:encoded><![CDATA[<p>
On one hand it&#8217;s kind of disheartening how much resistance and fear there
is towards objects from segments of the PHP community. (hilighted by the recent
hostility towards PHP5&#8242;s adding of something resembling a working object model)
On the other hand can you blame them when popular tools like Smarty <a
href="http://smarty.php.net/manual/en/advanced.features.php#advanced.features.ob
jects">punish you for using object?</a> 
</p> 

<p><p>
<h3>Transparent, Unified Access</h3>
In the ideal world you could pass an array or an object to <a href="http://smarty.php.net">Smarty</a> and have the access be transparent.  Being able to call methods on objects from your template is immensley useful.  Besides reinforcing your data model you also:
<ol>
  <li>don&#8217;t have to write all those toArray() method to dump your data to Smarty</li>
  <li>can lazy load data as needed</li>
  <li>can provide intelligent formatting without the need to write a Smarty plugin</li>
</ol>
And being able to do it transparently means it is painless to migrate from  using arrays to using objects (e.g. as your traffic grows, you switch to lazy loading objects), or from objects to arrays.  (another way to say it is, having to know the underlying data storage implementation is a clear case of a leaky abstraction)
</p>
<p>
(fyi this &#8220;ideal world&#8221; is also known as <a href="http://template-toolkit.org">Template Toolkit</a>)
</p>
<p>
<h3>SPL</h3>
There is however a way, with a little effort, to achieve this behaviour with Smarty and PHP5.  One of the most useful, and least discussed new features of PHP5, is the <a href="http://www.zend.com/manual/ref.spl.php">SPL</a>, &#8220;a collection of interfaces and classes that are meant to solve standard problems&#8221;, including allowing objects to behave like arrays, using the <a href="http://www.php.net/~helly/php/ext/spl/interfaceArrayAccess.html">ArrayAccess</a> interface.
</p>
<p>
<h3>A quick example (there are probably better ones)</h3>
</p>
<p>
Say you had a class that you used to help you page through a list of results, and at the bottom of each page you wanted to add a slug like &#8220;Widgets 21-30 of 142&#8243;.
</p>
<p>
One solution would be to assign <code>{$objectType}</code>, <code>{$showingMin}</code>, <code>{$showingMax}</code>, <code>{$totalToShow}</code> (or some such) variables to Smarty, and then have something like the following in your template:
<pre class="code">
{$objectType} {$showingMin}-{$showingMax} of {$totalToShow} in
</pre>
</p>
<p>
But to my eye that seems to clutter up the paging code with assigns, and clutter up the templates with unintuitive markup. (and this is a simple case)
</p>
<p>
Another solution would be to build the string at some point in your code, and pass <b>that</b> to Smarty (making your eventual I18N process just that much more challenging)
</p>
<p>
The third solution is to have your Pager class <em>pretend</em> to be an array, allowing you to add <code>{$page.showStatus}</code> to your templates which Smarty will compile into  <code>$this-><em>tpl</em>vars['page']['showStatus']</code>, and the SPL will intercept and transform into <code>$page->offsetGet('showStatus')</code>;
</p>
<p>
So whats does the Pager class look like?  Well the relevant bits look like:
<pre>
class Pager implements ArrayAccess {
   ...
   function offsetGet($k) {
    if ($this->offsetExists($k)) {
                return $this->$k();
        }
   }</p>

<p>function offsetExists($k) {
       return method_exists($this, $k);
   }</p>

<p>/* set and unset don't do anything */<br />
   function offsetSet($k, $v) {}
   function offsetUnset($k) {}</p>

<p>function showStatus() {
      ...construct status string and return it....
   }
}
</pre>
The offsetGet() method is the intersting piece.  It says, &#8220;take the array access key, and call the method of the same name on that object&#8221;.  Voila, you can now pass objects to templates, and call methods on them. 
</p>
<p>
<h3>A Word About Security</h3>
Generally you trust your template editors, but if calling arbitrary code worries you there are a couple of things you can do.
<ul>
<li>strip all non-word characters from your access key like so:  <code>$key = preg_replace('/\W/', '', $key);</code></li></p>

<p><li> the method calls are already namespaced to the object, but you could quarantine them further by appending a string to them (e.g. &#8216;tmpl<em>&#8216;), in which case your offsetGet() becomes
<pre>
   function offsetGet($k) {
        $k = preg</em>replace('/\W/', '', $k);
    $f = "tmpl<em>$f";
        return $this->$f();
   }
</pre>
(alternately you could use Relection API to limit offsetGet to only exposing methods inherited from some base class, or whatever other creative solution strikes your fancy)</li>
</ul>
</p>
<p>
(Note most of this code was sketched out on my way home tonight, and my spiral bound notebook&#8217;s PHP syntax hilighting is pretty weak not to mention lacks PHP5 support, so there might be some bugs in the above code)
</p>
<p>
<h3>Smarty and Iterators</h3>
Okay, so all that above is about using SPL&#8217;s array interface to allow method access, but what if you want pass iterators to Smarty?  Should just work right?  Unfortunately not.  You see in Smarty&#8217;s <code></em>compile<em>foreach</em>start</code>, arrays are accessed with an <b>explicit</b> array cast, making it impossible to use an iterator.   The quick and dirty solution is to remove this explicit cast.  However Smarty&#8217;s vmethod&#8217;s list .first, and .last won&#8217;t be available.  For that you need this <a href="http://news.php.net/php.smarty.dev/2510">patch sent to the Smarty Dev list</a>. (hasn&#8217;t tested it yet)
</p></p>
]]></content:encoded>
			<wfw:commentRss>http://laughingmeme.org/2004/08/05/simple-object-access-from-smarty/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MagpieRSS 0.4</title>
		<link>http://laughingmeme.org/2003/01/20/magpierss-04/</link>
		<comments>http://laughingmeme.org/2003/01/20/magpierss-04/#comments</comments>
		<pubDate>Mon, 20 Jan 2003 18:44:00 +0000</pubDate>
		<dc:creator>Kellan</dc:creator>
				<category><![CDATA[magpie]]></category>
		<category><![CDATA[smarty]]></category>

		<guid isPermaLink="false">http://lm.quxx.info/?p=277</guid>
		<description><![CDATA[Slouching towards release, part 2 Also, I released MagpieRSS 0.4 today. New and better error handling. Its not very PHP like, but its rationale, and flexible, and designed to be used as part of a larger application. (like I said, not very PHP like) more and better examples on how to use Magpie, including how [...]]]></description>
			<content:encoded><![CDATA[<p>
<h3>Slouching towards release, part 2</h3>

Also, I released 
<a
href="http://sourceforge.net/project/showfiles.php?group_id=55691&#038;release_id=134850">
MagpieRSS 0.4</a> today. 

<ul>
<li>New and better error handling.  Its not very PHP like, but its rationale, and flexible, and designed to be used as part of a larger application. (like I said, not very PHP like)</li>
<li>more and better examples on how to use Magpie, including how to use it with <a href="http://smarty.php.net">Smarty</a></li>
<li>Ships with a custom Smarty plugin for parsing RSS dates so you can
include in your templates the nifty construct:
<blockquote>
<code>$item.dc.date|rss_date_parse|date_format:"%A, %B %e, %Y"}</code>
</blockquote>
</li>
</ul>
<a href="http://sourceforge.net/project/showfiles.php?group_id=55691&#038;release_id=134850">Download Mapie 0.4</a>.
</p>
]]></content:encoded>
			<wfw:commentRss>http://laughingmeme.org/2003/01/20/magpierss-04/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Domain Specific Templates</title>
		<link>http://laughingmeme.org/2002/12/22/domain-specific-templates/</link>
		<comments>http://laughingmeme.org/2002/12/22/domain-specific-templates/#comments</comments>
		<pubDate>Sun, 22 Dec 2002 18:39:46 +0000</pubDate>
		<dc:creator>Kellan</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[dsl]]></category>
		<category><![CDATA[moveable type]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[smarty]]></category>
		<category><![CDATA[templating]]></category>

		<guid isPermaLink="false">http://lm.quxx.info/?p=253</guid>
		<description><![CDATA[Rasterweb, whilst musing on PHP and Perl asked: I think the biggest strength of a system like Movable Type is that the templating system is so powerful and easy to use. Is there any PHP based CMS/weblogging tool that uses templates in a way even close to how Movable Type does? To which aaron responded: [...]]]></description>
			<content:encoded><![CDATA[<p>Rasterweb, whilst 
<a href="http://zymm.com/raster/200212.html#12202002075144">musing on PHP and
Perl asked</a>:</p>

<blockquote>
  <p>I think the biggest strength of a system like Movable Type is that the
  templating system is so powerful and easy to use. Is there any PHP based
  CMS/weblogging tool that uses templates in a way even close to how Movable Type
  does?</p>
</blockquote>

<p>To which 
<a href="http://aaronland.info/weblog/archive/4762">aaron responded:</a></p>

<blockquote>
  <p>php does have a decent template system called &#8216;Smarty&#8217;</p>
</blockquote>

<p>And I&#8217;ll be the first to step up and agree, 
<a href="http://smarty.php.net/">Smarty</a> is a fine tool, it lacks the
amazing flexibility and elegance of 
<a href="http://www.tt2.org">Template Toolkit</a>, but thats frosting, and it
stacks up nicely compared to 
<a href="http://jakarta.apache.org/velocity/">Velocity</a> and 
<a href="http://www.cheetahtemplate.org/">Cheetah</a>. (<a href="http://laughingmeme.org/work/archives/001728.html" title="Comparing Smarty and Template Toolkit">more on this theme</a>)</p>

<p>That said, its nothing remotely like 
<a href="http://www.movabletype.org/docs/mtmanual_tags.html#template%20tags">Moveable Type&#8217;s template system</a>, which I
find novel and nearly perfect.  As a programmer, with Template Toolkit at my
fingers, I tend to think of a template as procedural language for describing
presentation.  This is the same idea behind PHP and JSP.  </p>

<p>MT does something
totally different, it defines a 
<a href="http://www.google.com/search?q=domain+specific+language">domain specific</a> templating language for laying
out a blog.  I&#8217;ve read about domain specific languages in school, but its not
until seeing both how elegant, and powerful it can be did I finally understand
the appeal. (I&#8217;m still not willing to concede the point that Haskell is just
plain weird)  This is the same idea as JSP&#8217;s Taglibs, but without the ugly
syntax.</p>

<p>I occasionally run into the limits MT&#8217;s language, wishing I had a more flexible
conditional, or not operator, but it makes more sense then any approach I&#8217;ve
seen, and I&#8217;m trying to incorporate it into the Protest.net rewrite.</p>

<p>ps. PHP does have ImageMagick bindings, check out the new <a
href="http://magick.communityconnect.com/">PHP Imagemagick extension</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://laughingmeme.org/2002/12/22/domain-specific-templates/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

