title: Creating a custom PHP template
body: <p>I created a custom PHP templating system and the way I built it seems, well, inefficient. The three main goals for my template was to:</p>
  
  <ol>
  <li>Pull all site-wide HTML elements from a template.tpl include.</li>
  <li>Be able to dynamically assign content in the template.tpl (like <code>&lt;title&gt;</code> or <code>&lt;script&gt;</code>)</li>
  <li>Be as efficient and scalable as possible.</li>
  </ol>
  
  <p>In the end, my template system looked something like this:</p>
  
  <p><strong>randomPage.php</strong></p>
  
  <pre><code>&lt;?php
  // declare any page specific resources
  $pageTitle = "Random Sub-Title";
  $pageResources = "/css/someRandomCSS.css"
  $pageContent = "/randomPage.tpl"
  // include the generic page template
  include dirname($_SERVER['DOCUMENT_ROOT']).'/includes/template.tpl'
  ?&gt;
  </code></pre>
  
  <p><strong>randomPage.tpl</strong></p>
  
  <pre><code>&lt;h1&gt;&lt;?=$pageTitle?&gt;&lt;/h1&gt;
  &lt;p&gt;Some random page's content&lt;/p&gt;
  </code></pre>
  
  <p><strong>template.tpl</strong></p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
  &lt;head&gt;
     &lt;title&gt;My Site -- &lt;?=$pageTitle?&gt;&lt;/title&gt;
     &lt;link href="/css/styles.css" rel="stylesheet" type="text/css"&gt;
     &lt;link href="&lt;?=pageResources?&gt;" rel="stylesheet" type="text/css"&gt;
  &lt;/head&gt;
  &lt;body&gt;
     &lt;? include $pageContent ?&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>The main problem with this system is that for every web page, I need to manage <em>two</em> files: one for logic/data and the other for the page template. This seems largely inefficient to me, and doesn't seem like a very scalable approach.</p>
  
  <p>Recently, I come across the smarty framework, which would allow me to consolidate my system from randomPage.php and randomPage.tpl into something like:</p>
  
  <p><strong>randomSmartyPage.php</strong></p>
  
  <pre><code>{extends file="template.tpl"}
  {block name=pageTitle}My Page Title{/block}
  {block name=pageResources}
     &lt;link href="/css/someRandomCSS.css" rel="stylesheet" text="text/css"&gt;
  {/block}
  {block name=pageContent}My HTML Page Body goes here{/block}
  </code></pre>
  
  <p>Seeing this approach raised three major questions for me:</p>
  
  <ol>
  <li>Are there any fundamental flaws with how I am approaching my templating system?</li>
  <li>Can my original php code be refactored so I don't have to create two files for every web page?</li>
  <li>Would using the smarty (or perhaps an alternative framework) be a good idea in this case? </li>
  </ol>