title: PHP Looping Template Engine - From Scratch
body: <p>For a group project I am trying to create a template engine for PHP for the people less experienced with the language can use tags like {name} in their HTML and the PHP will replace that tag with a predefined variable from an array. As well as supporting loops.</p>
  
  <p>This is well beyond the expectations of the project, but as I have experience with PHP I thought it would be a good challenge to keep me busy!</p>
  
  <p>My main questions are, how do I do the loop part of the parser and is this the best way to implement such a system. Before you just recommend an existing template system, I would prefer to create it myself for experience and because everything in our project has to be our own.</p>
  
  <p>At the moment the basic parsing is done with regex and preg_replace_callback, it checks if $data[name] exists and if it does replaces it.</p>
  
  <p>I have tried to do the loop a variety of different ways but am not sure if I am on the correct track!</p>
  
  <p>An example if the data the parsing engine was given is:</p>
  
  <pre><code>Array
  (
      [title] =&gt; The Title
      [subtitle] =&gt; Subtitle
      [footer] =&gt; Foot
      [people] =&gt; Array
          (
              [0] =&gt; Array
                  (
                      [name] =&gt; Steve
                      [surname] =&gt; Johnson
                  )
  
              [1] =&gt; Array
                  (
                      [name] =&gt; James
                      [surname] =&gt; Johnson
                  )
  
              [2] =&gt; Array
                  (
                      [name] =&gt; josh
                      [surname] =&gt; Smith
                  )
  
          )
  
      [page] =&gt; Home
  )
  </code></pre>
  
  <p>And the page it was parsing was something like:</p>
  
  <pre><code>&lt;html&gt;
  &lt;title&gt;{title}&lt;/title&gt;
  &lt;body&gt;
  &lt;h1&gt;{subtitle}&lt;/h1&gt;
  {LOOP:people}
  &lt;b&gt;{name}&lt;/b&gt; {surname}&lt;br /&gt;
  {ENDLOOP:people}
  &lt;br /&gt;&lt;br /&gt;
  &lt;i&gt;{footer}&lt;/i&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>It would produce something similar to:</p>
  
  <pre><code>&lt;html&gt;
  &lt;title&gt;The Title&lt;/title&gt;
  &lt;body&gt;
  &lt;h1&gt;Subtitle&lt;/h1&gt;
  &lt;b&gt;Steve&lt;/b&gt; Johnson&lt;br /&gt;
  &lt;b&gt;James&lt;/b&gt; Johnson&lt;br /&gt;
  &lt;b&gt;Josh&lt;/b&gt; Smith&lt;br /&gt;
  &lt;br /&gt;&lt;br /&gt;
  &lt;i&gt;Foot&lt;/i&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Your time is incredibly appreciated with this!</p>
  
  <p>Many thanks,</p>
  
  <p>P.s. I completely disagree that because I am looking to create something similar to what already exists for experience, my well formatted and easy to understand question gets down voted.</p>
  
  <p>P.p.s It seems there is a massive spread of opinions for this topic, please don't down vote people because they have a different opinion to you. Everyone is entitled to their own!</p>