<?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"
	>

<channel>
	<title>Mindblind &#187; phpSprockets</title>
	<atom:link href="http://mindblind.net/tags/phpsprockets/feed/" rel="self" type="application/rss+xml" />
	<link>http://mindblind.net</link>
	<description>making web development suck less</description>
	<pubDate>Fri, 08 Feb 2008 06:23:39 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
	<language>en</language>
			<item>
		<title>How to Easily Manage HTML Output in PHP</title>
		<link>http://mindblind.net/2008/01/20/how-to-easily-manage-html-output-in-php/</link>
		<comments>http://mindblind.net/2008/01/20/how-to-easily-manage-html-output-in-php/#comments</comments>
		<pubDate>Sun, 20 Jan 2008 14:56:43 +0000</pubDate>
		<dc:creator>Jon Canady</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<category><![CDATA[phpSprockets]]></category>

		<guid isPermaLink="false">http://mindblind.net/2008/01/21/how-to-easily-manage-html-output-in-php/</guid>
		<description><![CDATA[

I thought I&#8217;d come out with the big guns for our inaugural post. If you&#8217;ve ever written a PHP-powered site, you&#8217;ll know that the mix of PHP and HTML in the same document can quickly become hideous to read and a nightmare to maintain.  

The usual solution is o use a template engine like [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://warsaw.innova-partners.com/~innova/sprockets.gif" alt="" title="Sprockets Logo" /></p>

<p>I thought I&#8217;d come out with the big guns for our inaugural post. If you&#8217;ve ever written a PHP-powered site, you&#8217;ll know that the mix of PHP and HTML in the same document can quickly become hideous to read and a nightmare to maintain.  </p>

<p>The usual solution is o use a template engine like <a href="http://www.smarty.net">Smarty</a>, but these are hard to learn and are overkill for smaller projects.</p>

<p>What&#8217;s a developer to do?</p>

<p><span id="more-4"></span></p>

<p>Enter <a href="http://phpsprockets.googlecode.com">phpSprockets</a>, a library written at <a href="http://innova-partners.com/blog">Innova Partners</a> in Columbus OH.  Take a normal PHP document that loops over database records and outputs a table:</p>


<div class="wp_syntax"><div class="code"><pre class="php">&lt;table&gt;
	&lt;tr&gt;
		&lt;th&gt;Name&lt;/th&gt;
		&lt;th&gt;Address&lt;/th&gt;
	&lt;/tr&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #808080; font-style: italic;">// assume $rows has been fetched from the database</span>
<span style="color: #0000ff;">$class</span> = <span style="color: #ff0000;">'alt'</span>;
&nbsp;
<span style="color: #b1b100;">foreach</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$rows</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$row</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$class</span> == <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #0000ff;">$class</span> = <span style="color: #ff0000;">'alt'</span>;
	<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #0000ff;">$class</span> = <span style="color: #ff0000;">''</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">?&gt;</span>
	&lt;tr <span style="color: #000000; font-weight: bold;">class</span>=<span style="color: #ff0000;">&quot;&lt;?php echo $alt ?&gt;&quot;</span>&gt;
	&lt;td&gt;&lt;?php <span style="color: #000066;">echo</span> <span style="color: #000066;">ucwords</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$row</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'name'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span> ?&gt;&lt;/td&gt;
	&lt;td&gt;&lt;?php <span style="color: #000066;">echo</span> <span style="color: #0000ff;">$row</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'address'</span><span style="color: #66cc66;">&#93;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> &lt;/td&gt;
	&lt;/tr&gt;
	<span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000066;">echo</span> <span style="color: #ff0000;">&quot;&lt;/table&gt;&quot;</span>;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>


<p>That&#8217;s a lot of code for a simple task, and it&#8217;s already a mess.  We&#8217;re applying a different class to alternating rows, we&#8217;re jumping into PHP from HTML, and if we need to do anything complex to this or add more rows this will quickly become unmaintainable.  If you had to work with code like this, you&#8217;d hate your job eventually.</p>

<p>Let&#8217;s see the same code in phpSprockets:</p>


<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #0000ff;">$table</span> = <span style="color: #000000; font-weight: bold;">new</span> Sprocket<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'table'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #0000ff;">$headers</span> = <span style="color: #0000ff;">$table</span>-&gt;<span style="color: #006600;">tr</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$headers</span>-&gt;<span style="color: #006600;">th</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Name'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$headers</span>-&gt;<span style="color: #006600;">th</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Address'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #0000ff;">$class</span> = <span style="color: #ff0000;">'alt'</span>;
&nbsp;
<span style="color: #b1b100;">foreach</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$rows</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$row</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
&nbsp;
	<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$class</span> == <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #0000ff;">$class</span> = <span style="color: #ff0000;">'alt'</span>;
	<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #0000ff;">$class</span> = <span style="color: #ff0000;">''</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #0000ff;">$tr</span> = <span style="color: #0000ff;">$table</span>-&gt;<span style="color: #006600;">tr</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$class</span> != <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #0000ff;">$tr</span>-&gt;<span style="color: #006600;">class</span> = <span style="color: #0000ff;">$class</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #0000ff;">$tr</span>-&gt;<span style="color: #006600;">td</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$row</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'name'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #0000ff;">$tr</span>-&gt;<span style="color: #006600;">td</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$row</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'address'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>


<p>Okay, the phpSprockets code is longer, but you never leave PHP-mode, and everything&#8217;s very readable.  I didn&#8217;t comment either chunk of code, but once you&#8217;ve learned the simple phpSprockets syntax you&#8217;ll find that it&#8217;s much easier to go back in five months and make changes, which isn&#8217;t something you can say about stock PHP without being very rigorous about coding style.  Since starting with Innova Partners I&#8217;ve switched to using phpSprockets for every PHP project I work on, and the benefits are remarkable.</p>

<p>This is an intentionally brief introduction.  For a more in-depth look watch the <a href="http://innova-partners.com/blog/2007/10/23/intro-to-phpsprockets/">introductory screencast</a> (by me!), and for full documentation and to download, visit our <a href="http://phpsprockets.googlecode.com">downloads</a> page.</p>
<span class="akst_link"><a href="http://mindblind.net/?p=4&amp;akst_action=share-this"  title="E-mail this, post to del.icio.us, etc." id="akst_link_4"  class="akst_share_link">Share This</a>
</span>]]></content:encoded>
			<wfw:commentRss>http://mindblind.net/2008/01/20/how-to-easily-manage-html-output-in-php/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
