title: Simple PHP template class
body: <p>I wanted separate my HTML from PHP so after searching I found a great little php class that just do the trick. Only issue that I try merging 2 templates together but it doesn’t work.</p>
  
  <p>Here is the original class that i found from below website</p>
  
  <p><a href="http://www.broculos.net/2008/03/how-to-make-simple-html-template-engine.html#.WCsa8CTy2ng" rel="nofollow noreferrer">http://www.broculos.net/2008/03/how-to-make-simple-html-template-engine.html#.WCsa8CTy2ng</a></p>
  
  <pre><code>class Template {
      /**
       * The filename of the template to load.
       *
       * @access protected
       * @var string
       */
      protected $file;
  
      /**
       * An array of values for replacing each tag on the template (the key for each value is its corresponding tag).
       *
       * @access protected
       * @var array
       */
      protected $values = array();
  
      /**
       * Creates a new Template object and sets its associated file.
       *
       * @param string $file the filename of the template to load
       */
      public function __construct($file) {
          $this-&gt;file = $file;
      }
  
      /**
       * Sets a value for replacing a specific tag.
       *
       * @param string $key the name of the tag to replace
       * @param string $value the value to replace
       */
      public function set($key, $value) {
          $this-&gt;values[$key] = $value;
      }
  
      /**
       * Outputs the content of the template, replacing the keys for its respective values.
       *
       * @return string
       */
      public function output() {
          /**
           * Tries to verify if the file exists.
           * If it doesn't return with an error message.
           * Anything else loads the file contents and loops through the array replacing every key for its value.
           */
          if (!file_exists($this-&gt;file)) {
              return "Error loading template file ($this-&gt;file).&lt;br /&gt;";
          }
          $output = file_get_contents($this-&gt;file);
  
          foreach ($this-&gt;values as $key =&gt; $value) {
              $tagToReplace = "[@$key]";
              $output = str_replace($tagToReplace, $value, $output);
          }
  
          return $output;
      }
  
      /**
       * Merges the content from an array of templates and separates it with $separator.
       *
       * @param array $templates an array of Template objects to merge
       * @param string $separator the string that is used between each Template object
       * @return string
       */
      static public function merge($templates, $separator = "\n") {
          /**
           * Loops through the array concatenating the outputs from each template, separating with $separator.
           * If a type different from Template is found we provide an error message. 
           */
          $output = "";
  
          foreach ($templates as $template) {
              $content = (get_class($template) !== "Template")
                  ? "Error, incorrect type - expected Template."
                  : $template-&gt;output();
              $output .= $content . $separator;
          }
  
          return $output;
      }
  }
  </code></pre>
  
  <p>Code that work</p>
  
  <pre><code>$post = new Template("post.tpl");
  $post-&gt;set("post_title", $post_title);
  $post-&gt;set("post_description", $post_description);
  $post-&gt;set("content", $post-&gt;output());
  echo $post-&gt;output();
  </code></pre>
  
  <p>Even when I want to loop if I add the code it works fine. But then I try to merge two template files together</p>
  
  <p>all_posts.tpl</p>
  
  <pre><code>&lt;div class=”posts”&gt;
  &lt;h1&gt;[@page_title]&lt;/&lt;/h1&gt;
  [@display_posts]
  &lt;/div&gt;
  display_posts.tpl
  &lt;div class=”post”&gt;
  
  &lt;h2&gt;[@display_title]&lt;/h2&gt;
  
  &lt;p&gt;[@display_description]&lt;/p&gt;
  
  &lt;/div&gt;
  </code></pre>
  
  <p>So what I want to do now is to push display_posts.tpl to all_posts.tpl and replace the tag [@display_posts]</p>
  
  <p>So in my php I did below</p>
  
  <pre><code>$post = new Template("all_posts.tpl ");
  $post-&gt;set("page_title", "All Posts");
  echo $post-&gt;output();
  
  //created a array from a mysqli loop
  
  $post_set  = array();
  
  while ($post_row = mysqli_fetch_array($posts)){
  
      $post_title = $post_row['title'];
      $post_description = $post_row['description'];
  
      $post_set[] = array(
          "display_title" =&gt; $post_title, 
          "display_description" =&gt; $post_description
  );
  
  }
  
  
  
  foreach ($post_set as $posts) {
          $row = new Template("list_users_row.tpl");
  
          foreach ($posts as $key =&gt; $value) {
              $row-&gt;set($key, $value);
          }
          $postsTemplates[] = $posts;
      }
  
  // here i try to merge template 
  
  $postsContents = Template::merge($postsTemplates);
  
  $layout-&gt;set("content", $postsContents-&gt;output());
  
  echo $layout-&gt;output();
  </code></pre>
  
  <p>But this is throwing off a error set() is not an function. Could someone help me out to figure out this? I’m still in the process of learning php classes. </p>