Gast
Diese Klasse hier stellt einen simplen Template Parser da.
Das Ganze ist auch nochmal mit Beispiel Skript im Dateianhang vorhanden.
So Update mit Caching.
Die Alte und die neue Version inkl. Beispiele findet man hier:
http://dev.php-elite.de/
PHP
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:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
<?php
/**
* Klasse template
*
* @author php-elite.de <admin@php-elite.de>
* @version 0.1
* @copyright www.php-elite.de
*
* Anwendungs Beispiel:
* ====================
* <?php
* 1. $tpl = new template("/var/www/html/design1.html"); Ohne Caching
* 2. $tpl = new template("/var/www/html/design1.html", FALSE); Ohne Caching
* 3. $tpl = new template("/var/www/html/design1.html", TRUE); Mit Caching
* $tpl -> variable("benutzername", $username); # Nutzung im Template File Über {-benutzername-}
* $tpl -> datei("header", "/var/www/html/header.html"); # Nutzung im Template File Über {-include:header-}
* echo $tpl -> ausgeben();
* ?>
*/
if (! defined ( "CACHEN" )) {
define ( "CACHEN" , FALSE );
}
if (! defined ( "CACHE_DAUER" )) {
define ( "CACHE_DAUER" , 3600 );
}
if (! defined ( "DS" )) {
define ( "DS" , DIRECTORY_SEPARATOR );
}
class template {
private $ersetzen = array(array());
private $templatedatei = NULL ;
private $cache_name = NULL ;
private $cachen = CACHEN ;
public $tpl_inhalt = NULL ;
public $vor_variable = '{-' ;
public $nach_variable = '-}' ;
public $vor_datei = '{-include:' ;
public $nach_datei = '-}' ;
/**
* Neues Template erstellen.
* @param <string> $templatedatei Absoluter Pfad zur Template Datei.
*/
public function __construct ( $templatedatei = '' , $cachen = CACHEN ) {
$this -> cachen = $cachen ;
if ( file_exists ( $templatedatei )) {
$this -> templatedatei = $templatedatei ;
$this -> variable ( "template_parser_version" , '0.1' );
} else {
$this -> fehler ( 'Das Template <b>' . $templatedatei . '</b> existiert nicht.' );
exit();
}
}
/**
* Variable hinzufügen
* @param <string> $name Name der zu erstellende Template Variable.
* @param <string> $wert Wert der zu erstellende Template Variable.
* @return <BOOL> Wenn Variable erstellt wurde, wird TRUE zurück gegeben, ansonsten FALSE.
*/
public function variable ( $name = '' , $wert = '' ) {
if (!empty( $name )) {
$anzahl = ( count ( $this -> ersetzen , COUNT_RECURSIVE )/ 2 - 1 );
$this -> ersetzen [ 0 ][ $anzahl ] = $this -> vor_variable . $name . $this -> nach_variable ;
$this -> ersetzen [ 1 ][ $anzahl ] = $wert ;
return TRUE ;
} else {
return FALSE ;
}
}
/**
* Fehler ausgeben und weitere Skriptausführung unterbrechen.
*
* @param <string> $text Fehler Text.
*/
private function fehler ( $text = '' ) {
echo $text ;
exit();
}
/**
* Datei hinzufügen
* @param <string> $name Name der Datei die eingebunden werden soll.
* @param <string> $datei Absolute Adresse zur Datei die im template intigriert werden soll.
* @return <BOOL> Wenn die Datei erstellt wurde, wird TRUE zurück gegeben, ansonsten FALSE.
*/
public function datei ( $name = '' , $datei = '' ) {
if (!empty( $name ) and file_exists ( $datei )) {
$anzahl = ( count ( $this -> ersetzen , COUNT_RECURSIVE )/ 2 - 1 );
$this -> ersetzen [ 0 ][ $anzahl ] = $this -> vor_datei . $name . $this -> nach_datei ;
$this -> ersetzen [ 1 ][ $anzahl ] = file_get_contents ( $datei );
return TRUE ;
} else {
return FALSE ;
}
}
/**
* Template Parsen
*/
private function parsen () {
$this -> tpl_inhalt = file_get_contents ( $this -> templatedatei );
$this -> tpl_inhalt = str_replace ( $this -> ersetzen [ 0 ], $this -> ersetzen [ 1 ], $this -> tpl_inhalt );
}
/**
* Template Ausgeben
* @return <string> Geparstes Template wird zurück gegeben.
*/
public function ausgeben () {
if ( CACHEN and $this -> cachen ) {
$this -> cache_name = 'cache' . DS . md5 ( $this -> templatedatei ). '.html' ;
if ( file_exists ( $this -> cache_name ) and time () - filemtime ( $this -> cache_name ) < CACHE_DAUER ) {
return file_get_contents ( $this -> cache_name );
} else {
if ( is_writable ( 'cache' )) {
$this -> parsen ();
file_put_contents ( $this -> cache_name , $this -> tpl_inhalt );
return $this -> tpl_inhalt ;
} else {
$this -> fehler ( "Der Ordner <b>cache</b> ist nicht beschreibbar." );
}
}
} else {
$this -> parsen ();
return $this -> tpl_inhalt ;
}
}
}
Post wurde schon 10x editiert, das letzte mal am 29.06.2010 um 19:58 von
19.06.2010, 20:47
Mogria
Mitglied
Sehr guter User
Dabei seit: 30.04.2010
Herkunft: Schweiz
Posts: 614
Eine Frage:
PHP
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
<?php
public function variable ( $name = '' , $wert = '' ) {
if (!empty( $name ) and ! in_array ( $name , $this -> reserviert )) {
$anzahl = ( count ( $this -> ersetzen , COUNT_RECURSIVE )/ 2 - 1 );
$this -> ersetzen [ 0 ][ $anzahl ] = $this -> vor_variable . $name . $this -> nach_variable ;
$this -> ersetzen [ 1 ][ $anzahl ] = $wert ;
return TRUE ;
} else {
return FALSE ;
}
}
?>
Was bringt es, wenn man mit dieser Funktion eine Variable erstellt ?
19.06.2010, 21:34
Profil |
PM |
E-Mail
Gast
damit definiert man die Platzhalter,
lade mal das Beispiel Skript im Anhang runter, das verdeutlicht das ganz gut.
19.06.2010, 22:04
bgentwickler
Mitglied
Gruenling
Dabei seit: 30.10.2012
Herkunft: Deutschland
Posts: 21
dein beispiel ist nicht mehr im anhang und der link funktioniert auch nicht
11.11.2012, 17:00
Profil |
PM |
E-Mail