title: PHP - templating with custom tags - is this a legit use of eval?
body: <p><strong>Overview</strong></p>
  
  <p>Around the end of 2009, I wrote a simple templating system for PHP/HTML to be used in-house by our designers for brochure-ware type websites. The goal of the system is to allow templating in otherwise pure HTML via custom tags that are processed by PHP. For example, a templated page might look like this:</p>
  
  <pre><code>&lt;tt:Page template="templates/main.html"&gt;
    &lt;tt:Content name="leftColumn"&gt;
      &lt;p&gt; blah blah &lt;/p&gt;
      ...
    &lt;/tt:Content&gt;
    &lt;tt:Content name="rightColumn"&gt;
      &lt;p&gt; blah blah &lt;/p&gt;
      ...
    &lt;/tt:Content&gt;
  &lt;/tt:Page&gt;
  </code></pre>
  
  <p>The template itself might look something like this:</p>
  
  <pre><code>&lt;html&gt;
    &lt;head&gt;...&lt;/head&gt;
    &lt;body&gt;
      &lt;div style="float:left; width:45%"&gt;
        &lt;tt:Container name="leftColumn" /&gt;
      &lt;/div&gt;
      &lt;div style="width:45%"&gt;
        &lt;tt:Container name="rightColumn" /&gt;
      &lt;/div&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Besides the Page and Content/Container tags, there are a few other tags included in the core for stuff like flow control, iterating over a collection, outputting dynamic values, etc. The framework is designed so it's very easy to add your own set of tags registered under another prefix and namespace.</p>
  
  <p><strong>Custom Tags to PHP</strong></p>
  
  <p>How do we parse these custom tags? Since the're no guarantee that the HTML file is well-formed XML, solutions like XSLT/XPATH won't be reliable. Instead, we use a regex to look for tags with registered prefixes, and replace those with PHP code. The PHP code is a stack-based design... upon encountering an opening tag, an object representing the tag is created pushed onto the stack, and its "initialization function" (if any) runs. Whenever a registered closing tag is encountered, the most recent object is popped off the stack, and its "rendering function" runs.</p>
  
  <p>So, after the framework replaces the templating tags with PHP, our example page might look something like this (in realty it's a bit uglier):</p>
  
  <pre><code>&lt;?php $tags-&gt;push('tt', 'Page', array('template'=&gt;'templates/main.html')); ?&gt;
    &lt;?php $tags-&gt;push('tt', 'Content', array('name'=&gt;'leftColumn')); ?&gt;
      &lt;p&gt; blah blah &lt;/p&gt;
      ...
    &lt;?php $tags-&gt;pop(); ?&gt;
    &lt;?php $tags-&gt;push('tt', 'Content', array('name'=&gt;'rightColumn')); ?&gt;
      &lt;p&gt; blah blah &lt;/p&gt;
      ...
    &lt;?php $tags-&gt;pop(); ?&gt;
  &lt;?php $tags-&gt;pop(); ?&gt;
  </code></pre>
  
  <p><strong>The good, the bad, and <code>eval</code></strong></p>
  
  <p>Now, how to execute our newly-generated PHP code? I can think of a few options here. The easiest is to simply <code>eval</code> the string, and that works well enough. However, any programmer will tell you "eval is evil, don't use it..." so the question is, is there anything more appropriate than <code>eval</code> that we can use here? </p>
  
  <p>I've considered using a temporary or cached file, using <code>php://</code> output streams, etc, but as far as I can see these don't offer any real advantage over <code>eval</code>. Caching could speed things up, but in practice all the sites we have on this thing are already blazingly fast, so I see no need to make speed optimizations at this point.</p>
  
  <p><strong>Questions</strong></p>
  
  <p>For each of the things on this list: is it a good idea? Can you think of a better alternative?</p>
  
  <ul>
  <li>the whole idea in general (custom tags for html / php)</li>
  <li>converting tags to php code instead of processing directly</li>
  <li>the stack-based approach </li>
  <li>the use of <code>eval</code> (or similar)</li>
  </ul>
  
  <p>Thanks for reading and TIA for any advice. :)</p>