index.php
  1. <?
  2. include (&#092;"parser.php\");
  3. $tpl = new template;
  4. $tpl -> load(index.tpl);
  5. $tpl -> blocks = array(
  6. &#092;"TYTOL\" => \"Moja pierwsza strona z szablonami\",
  7. &#092;"MENU\" => \"DUPA\",
  8. &#092;"TRESC\" => \"hohohoho dziala:)\"
  9. );
  10. $tpl->parse();
  11. echo $tpl->parsed;
  12. ?>


index.tpl
  1. <?php
  2.  
  3. <html>
  4. <head>
  5. <TITLE>{TYTOL}</TITLE>
  6. </head>
  7. <body>
  8. <table>
  9. <tr><td>{MENU}</td><td>{TRESC}</td></tr>
  10. <table>
  11. </body>
  12. </html>
  13.  
  14. ?>


parser.php
  1. <?
  2. class template
  3. {
  4.    var $unparsed = array(0 => '');
  5.    var $parsed = '';
  6.    var $blocks = array('default' => '');
  7.  
  8.  function load($plik)
  9.    {
  10.       $this -> unparsed = @file($plik);
  11.    }
  12.   function parse()
  13.    {
  14.       $this -> parsed = &#092;"\";
  15.       $cnt = count($this -> unparsed);
  16.       for($i = 0; $i <= $cnt; $i++)
  17.       {
  18.          $tekst = $this -> unparsed[$i];
  19.          $found = array();
  20.          if(preg_match_all(&#092;"#{(.+?)}#is\", $tekst, $found))
  21.          {
  22.             foreach($found[1] as $block)
  23.             {
  24.                $block_names[] = '{'.$block.'}';
  25.                $block_values[] = &$this -> blocks[$block]; 
  26.             }
  27.             $tekst = str_replace($block_names, $block_values, $tekst);
  28.          }
  29.          $this -> parsed .= $tekst;
  30.       }
  31.    }
  32. }
  33. ?>


Moja glupota... juz dziala ;/