n0ooo0b
Goto Top

Einfache Template-Klasse

Mit dieser einfachen Template-Klasse können Sie PHP von HTML getrennt halten

Hallo Benutzer!

Ich stelle dir diese einfache Template-Klasse gerne zuverfügung!

back-to-toptemplate.class.php:


<?php
class template
{

  /**
    * Template Inhalt
    *
    * @access protected
    * @var string 
   */
   protected $tmpl_file;

  /**
    * Template variabeln
    *
    * @access protected
    * @var array 
   */
   protected $tmpl_vars; 
   
     /**
     * Template - Datei auslesen
     * @param string $template Template-Name
     */
     public function __construct($template) {
          if (file_exists($template)) {
               $this->tmpl_file = file_get_contents($template);
          }else {
               die("Template not exists! dummy: {$template}");  
          }
     }

     /**
     * Template - Variabeln ersetzen bzw setzen
     * @param array or string $search 
     * @param string $replace
     */
     public function assign($search, $replace=NULL) {
          if (is_array($search)) {
              foreach($search as $key => $value) {
                $this->tmpl_vars[$key] = $value;
              }
          }else {
                $this->tmpl_vars[$search] = $replace;
          }
     }
     
     /**
     * Template - ausgeben
     * @return string
     */
     public function output() {
          
          $this->tmpl_file = preg_replace("/\{(.*?)\}/ise", "\$this->tmpl_vars['\\1']", $this->tmpl_file);  
              print($this->tmpl_file);
     }


}
?>

back-to-topindex.php:

<?php
  include("template.class.php"); //Includes Class  
   define("TPL_DIR", "templates/");  
$tmpl = new template(TPL_DIR."index.tpl");  

$tmpl -> assign("Inhalt", "Dein Text");  
$tmpl -> assign(array("Name" => "Dein Name", "Bild" => "Dein Bild"));  

$tmpl -> output(); //Template print
?>

back-to-topindex.tpl:

<?xml version="1.0"?>  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">  
<html>
  <head>
    <title>New Document</title>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />  
  </head>
  <body>
    {Inhalt}
<hr />
<b>{Name}</b><br />
<b>{Bild}</b>
  </body>
</html>

back-to-topAusgabe:

<?xml version="1.0"?>  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">  
<html>
  <head>
    <title>New Document</title>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />  
  </head>
  <body>
    Dein Text
<hr />

<b>Dein Name</b><br />
<b>Dein Bild</b>
  </body>
</html>

Content-Key: 121855

Url: https://administrator.de/contentid/121855

Printed on: May 4, 2024 at 20:05 o'clock

Member: nxclass
nxclass Aug 03, 2009 at 07:36:46 (UTC)
Goto Top
... ggf. kann man auch die Magischen Funktionen _ _ tostring(), _ _ get() und _ _ set() einsetzen.

Wenn man schon im Konstruktor die Ersetzungsmarken in ein Array einliest, könnte man diese über _ _ set($sKey, $sValue) {} setzen und über _ _ get($sKey) {} auslesen als währen es Variablen der Template Klasse.
// Keine Zeit, mach Du das ...  ;-)

... schön das an die Documentor Tags gedacht wurde!