PHP Templating: the easy and powerfull way…
Es gibt unzählige Template Engines und ich habe auch so manche ausprobliert. Und was ist das Fazit? Keine ist wirklich brauchbar! Denn
- ich will keine eine eigene Template-Sprache lernen
- ich will keine Templates kompilieren lassen
- ich will auch nicht hunderte Libraries einbinden müssen
Jedenfalls bin ich auf PHPGuru.org auf einen Artikel [1] gestossen und habe mir aus dem Konzept eine eigene Template Engines geschrieben die 41 Zeilen Code gross ist und einiges (wenn nicht mehr) kann als z.B. Smarty (wie Objekte in Templates oder HTML aus PHP wieder im Template verwenden). Aber seht es euch selber an unter templating.zip [2]. Kommentare erwünscht!
Template Beispiel
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <?include("header.tmpl.php")?> <h1><?=$title?></h1> <h2>This is a navigation</h2> <?=$navigation?> <h2>Counting</h2> <table> <?foreach($texts as $t):?> <tr> <td bgcolor="#dddddd"><?=$t?></td> </tr> <?endforeach?> </table> <h2>Persons</h2> <table> <?foreach($personobjs as $p):?> <tr> <td bgcolor="#dddddd"><?=$p->getName()?> </td> </tr> <?endforeach?> </table> <?include("footer.tmpl.php")?> |
Template Klasse
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | <?php class Template { private $output = ""; private $path = ""; private $send = array(); public function __construct($path = "") { $this->path = $path; } public function __set($name, $value) { $this->send[$name] = $value; } public function setPath($path) { $this->path = $path; } public function useTemplate($templatename) { $this->output = $this->getTemplateContent($this->path.$templatename); } private function getTemplateContent($filename) { foreach ($this->send as $key => $value) { $$key = $value; } if (is_file($filename)) { ob_start(); include $filename; $contents = ob_get_contents(); ob_end_clean(); return $contents; } return false; } public function getOutput() { return $this->output; } } |
Eine beispiel Seite
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | <?php require_once "DB.class.php"; require_once "Template.class.php"; // this is a site class class Site { private $DB; private $Template; public function __construct() { // inits db class $this->DB = new DB(); // inits template class $this->Template = new Template(); $this->Template->setPath("../templating/tmpl/"); } // shows a simple html site public function showIndex() { // gets content from db $texts = $this->DB->getTexts(); // sets content to template $this->Template->texts = $texts; //parses the content and use the output again in a template $this->Template->useTemplate("navigation.tmpl.php"); $this->Template->navigation = $this->Template->getOutput(); // sets another var to the template $this->Template->title = "hello world"; // gets Persons as an array of objects form the db and set it as a // var to the template $this->Template->personobjs = $this->DB->getPersons(); //parses the content $this->Template->useTemplate("page.tmpl.php"); // shows the output echo $this->Template->getOutput(); } } $Site = new Site(); $Site->showIndex(); |
[1] http://www.phpguru.org/static/templating.html
[2] http://www.renemoser.net/dev/templating.zip