frankx: Templateengine PHP MVC

Beitrag lesen

Hellihello Vinzenz,

merci!

das tut Code nie, auch nicht der irgendwelcher Programmiergenies.
Gerade wenn man eigenen Code anderen zumutet, sollte man dafür sorgen, dass Code vernünftig kommentiert ist. Da gab's letztlich einen schönen Link zu einem wunderbaren Weblogartikel - leider finde ich den gerade nicht :-(

Und dort dann auch: http://phpdoc.org/, http://www.stack.nl/~dimitri/doxygen/.

<?php
include("Small_Template_Handler.php");

// Warum include?
// Warum nicht require, genauer gesagt require_once?

besser so: ?
[code lang=php]

<?php
// get the required Class
require_once("Small_Template_Handler.php");

// set somt Testdata to test with
$test_data_array["name"] = "Müller";
$test_data_array["contact"] = "Telefon";
$test_data_array["since"] = "1.1.1800";

//choose Template File
$template_file_name = "test.template.html";
//create instance
$my_sth = new Small_Template_Handler($template_file_name, $test_data_array);
//produce output (echo replaced template)
$my_sth->putout();
?>

  
  

>   
> > $template = file\_get\_contents("test.template.html");  
> > $data["name"] = "Müller";  
> > $data["contact"] = "Telefon";  
> > $data["since"] = "1.1.1800";  
> > $output = Small\_Template\_Handler::handle($template, $data);  
>   
> // Was ist nun mit den statischen Eigenschaften Deiner Klasse?  
> // Läßt Du den Kram dort einfach unordentlich liegen?  
  
Mmh, dem kann ich nicht ganz folgen. Vielleicht ists ja schon unten repariert.  

>   
> > var\_dump($output);  
> > ?>  
>   
> > <?php  
> > // expects template as string and data as array  
> > // finds used placeholders machting pattern "{placeholder}"  
> > // replaces all found placeholders with values, if key exists in data-hash  
> > class Small\_Template\_Handler  
> > {  
> >  private static $template = ""; // template string given as parameter  
> >  private static $used\_placeholders = array(); //list of all found placeholders  
> >  private static $data = array();//data-hash (associative array where keys match placholders name)  
> >  private static $output = ""; //replaced template  
>   
> > [/code]  
>   
> Anders gefragt:  
> Warum willst Du unbedingt darauf verzichten, eine Objektinstanz zu erzeugen?  
> Was bezweckst Du mit der Doppelung $template und $output?  
  
Die Doppelung von Template und Output hatte ich vergenommen, weil ich das als zwei unterschiedliche Dinge ansah. Habs jetzt mal abgeschafft. Unbeding verzichten auf eine Objektinstanz wollte ich nicht. Ich sehe den Unterschied irgendwie nie wirklich und frage mich immer wieder, ob es einen Grund für das eine oder andere gibt. Habs jetzt auch mal umgebaut:  
  
~~~php
  
?php  
// expects template as string and data as array  
// finds used placeholders machting pattern "{placeholder}"  
// replaces all found placeholders with values, if key exists in data-hash  
class Small_Template_Handler  
{  
 private $used_placeholders = array(); //list of all found placeholders  
 private $data = array();//data-hash (associative array where keys match placeholders name)  
 private $output = ""; //replaced template  
  
  
 // sets class-vars corresponding to given parameters  
 // and calls both necessary functions  
  function __construct($template_file_path, $data)  
 {  
  //set output to content of template file  
  $this->output = file_get_contents($template_file_path);  
  $this->data = $data;  
  $this->find_used_placeholders();  
    $this->replace_placeholders ();  
 }  
  
 // finds used placeholders machting "{placeholder}"  
 // and stores them as array in class-variable $used_placeholders;  
 private function find_used_placeholders ()  
 {  
  $pattern = '/\{(.*?)\}/'; // pattern matching "{placeholder}"  
  $subject = $this->output; //  
  preg_match_all($pattern, $subject, $matches);  
  // get only one entry per used placeholder and store it in class variable  
  $this->used_placeholders = array_unique($matches[1]);  
 }  
  
 // loops class-variable $used_placeholders  
 // and replaces all found placeholders with values, if key exists in data-hash  
 private function replace_placeholders ()  
 {  
  foreach ($this->used_placeholders as $placeholder) //all found placeholders  
   {  
   if (isset($this->data[$placeholder])) //if key exists in data-hash  - replace {key==placeholder}  
   {  
    $search="{".$placeholder."}";  
    $replacement = $this->data[$placeholder];  
    $subject = $this->output;  
    $this->output = str_replace($search, $replacement, $subject);  
   }  
  }  
 }  
  
 public function putout()  
 {  
  var_dump($this->output);  
 }  
}  
?>  

Versprichst Du Dir eine Optimierung durch Dein Suchen, ob ein bestimmter Platzhalter vorhanden ist?

Naja, ich dachte, es wird dann schnell ersichtlich, ob ein Platzhalter keine Entsprechung im Daten-Hash hat. Zudem enthält der Daten-Hash bzw. die List of Hashes unter Umständen eine Menge mehr Variablen (keys), als im Template benötigt werden.

Warum verwendest Du nicht einfach die Array-Variante von str_replace(), die Du einfach mit allen Schlüssel-Wert-Paaren fütterst, d.h. dem Array der Schlüssel und dem Array der korrespondierenden Werte?

Du meinst die o.g. Funktion ersetzen durch

  
  
 private function alternative_replace_placeholders ()  
 {  
  $search = array();  
  $replacement = array();  
  foreach ($this->used_placeholders as $placeholder) //all found placeholders  
   {  
   //if key exists in data-hash  - fill arrays seach and replacement  with "{placeholder}" and value  
   if (isset($this->data[$placeholder]))  
   {  
    $search[]="{".$placeholder."}";  
    $replacement[] = $this->data[$placeholder];  
   }  
   $subject = $this->output;  
   $this->output = str_replace($search, $replacement, $subject);  
  }  
 }  

Ich will doch auch überprüfen, ob der Key existiert. Wenn nicht, ist ja was faul im Staate Dänemark. Oder?

Dank und Gruß,

Robert aka
frankx

--
tryin to multitain  - Globus = Planet != Welt