templateparser.class.php
<?php
/**
 * sTemplate class
 *
 * New way to use templates.
 *
 * @author Sebastian Potasiak
 * @version 2.1 BETA
 * @update 28-08-2009
 *
 * @increased at 02-12-2009
 * @increased by Henry van den Berg
 */

class sTemplate
{
   public $dir = "./templates/"; // Templates dir
   public $ext = ".html"; // Template files extension
   public $atr = "class"; // HTML attribute, which value is name of variable


   /**
    * show_error()
    *
    * Showing an error
    *
    * @param message [string] - Error message content
    *
    * @access private
    * @return string
    */
   private function show_error($message)
   {
      echo '<b>Error:</b> ' . $message . '<br /><br />';
      return $message;
   }


   /**
    * parse()
    *
    * Parsing template
    *
    * @param template [string] - Template filename
    * @param values [array] d:0 - Values to replace
    * @param settings [array] d:0 - Settings to replace
    *
    * @access public
    * @return string
    */
   public function parse($template, $values = 0, $settings = 0)
   {
      $path = $this->dir . $template . $this->ext;

      if (!file_exists($path) || !is_readable($path))
      {
         $this->show_error("File does not exists, or is not readable.");
      }

      $file = implode("", file($path));

      if ($values == 0 || !is_array($values)) return $result;

      $result = $this->settings($content, $settings);
      $result = $this->loop($content, $values);

      return $result;
   }


   /**
    * settings()
    *
    * Recursive loop, set the specified objects to its relevant value
    *
    * @param content [string] - Template content
    * @param settings [array] d:0 - Settings to replace
    *
    * @access private
    * @return string
    */
   private function settings($content, $settings = 0)
   {
      if ($settings == 0 || !is_array($settings)) return $content;

      foreach ($settings as $key => $value)
      {
         if (is_array($value))
         {
            foreach ($value as $subkey => $subvalue)
            {
               if (is_array($subvalue))
               {
                  foreach ($subvalue as $subsubkey => $subsubvalue)
                  {
                     $content = str_replace('{' . $key .':'. $subkey .':'. $subsubkey .'}', $subsubvalue, $content);
                  }
               }
               else
               {
                  $content = str_replace('{' . $key .':'. $subkey .'}', $subvalue, $content);
               }
            }
         }
         else
         {
            $content = str_replace('{' . $key . '}', $value, $content);
         }
      }

      return $content;
   }


   /**
    * loop()
    *
    * Recursive loop, which replacing data
    *
    * @param content [string] - Template content
    * @param values [array] d:0 - Values to replace
    *
    * @access private
    * @return string
    */
   private function loop($content, $values = 0)
   {
      if ($values == 0 || !is_array($values)) return $content;

      foreach ($values as $key => $value)
      {
         if (is_array($value))
         {
            preg_match(
                '/\<\!\-\-(\s?)loop ' . $key . ' start(\s?)\-\-\>(.*?)\<\!\-\-(\s?)loop ' . $key . ' end(\s?)\-\-\>/si',
                $content,
                $loop
            );
            $final_value = "";

            foreach ($value as $item)
            {
               $final_value .= $this->loop($loop[3], $item);
            }
                
            $content = preg_replace(
                '/\<\!\-\-(\s?)loop ' . $key . ' start(\s?)\-\-\>(.*?)\<\!\-\-(\s?)loop ' . $key . ' end(\s?)\-\-\>/si',
                $final_value,
                $content
            );
         }
         else
         {
            $content = preg_replace(
                 '/\<([a-zA-Z0-9\"\'\.=\s]+) ' . $this->atr . '=(\'|\"){1}' . $key . '([a-zA-Z0-9\{\}\"\'\.=\s]+)\>(.*?)\<\/([a-zA-Z0-9\"\'\.=\s]+)\>/si',
                 '<\\1 ' . $this->atr . '=\\2' . $key . '\\3>\\4' . $value . '</\\5>',
                  $content
            );
            $content = str_replace('{' . $key . '}', $value, $content);
         }
      }

      return $content;
   }
}
?>

[b]Voorbeeld[/b]

index.php
<?php
include('./resources/php/templateparser.class.php');

$arrConfig['application']['name'] = "CSS templates";

$arrConfig['module']['version'] = "0.1 BETA";
$arrConfig['module']['language'] = "NL";
$arrConfig['module']['copyright']['year'] = 2009;

$tpl = new sTemplate;

$tpl->dir = './'; // Use only if you have another directory name - this is default
$tpl->ext = '.tpl'; // Like dir - use if you have another
$tpl->atr = 'class'; // Like dir and ext - use if you want to use another attribute in html

// Create an array with variables
$data = array(
    "menu" => array(
        array(
            "item" => "Sinterklaas",
            "current" => "",
            "onclick" => ""
        ),
        array(
            "item" => "Zwarte Piet",
            "current" => "",
            "onclick" => ""
        ),
        array(            
            "item" => "zijn jarig",
            "current" => "current",
            "onclick" => "alert('Hey you!')"
        )
    )
);

// Show parsed template
$tpl->parse("index", $data, $arrConfig);
?>

index.tpl
<? //voor de kleurtjes
<html xml:lang="{module:language}">

<head>
   <title>{application:name}</title>

   <meta name="copyright" content="Copyright © {module:copyright:year}, all rights reserved" />
   <meta name="version" content="{module:version}" />

   <style>
   .item
   {
      color: #ff0000;
   }

   .item.current
   {
      color: #00ff00;
   }
   </style>
</head>

<body>
   <div class="menu">
      <!-- loop menu start -->
         <div class="item {current}" onclick="{onclick}"></div>
      <!-- loop menu end -->
   </div>
</body>

</html>
//voor de kleurtjes ?>