Nach der letzten Veröffentlichung der Simplet-Klasse ( http://bbs.phpchina.com/thread-85257-1-1.html ) wurde festgestellt, dass es viele Mängel gibt, z. B. keine Unterstützung mehrdimensionaler Arrays, Kommentare usw Caching-Funktion, dieses Mal füge ich all diese Funktionen hinzu! ! !
Wenn Sie interessiert sind, werde ich das nächste Mal ein Tutorial veröffentlichen, um über einige meiner Ideen und Methoden zum Schreiben dieses Kurses zu sprechen! ! ! Wenn du mich unterstützt, kannst du mir folgen und mich unterstützen!
PHP-Code:
simplet.class.php
<?php
Klasse SimpleT {
private $t_vars;
privates $templates_dir;
privat $templates_c_dir;
privater $cache;
privat $cache_dir;
öffentliche Funktion __construct() {
$this->templates_dir = './templates/';
$this->templates_c_dir = './templates_c/';
$this->cache = 0;
$this->cache_dir = './cache/';
}
öffentliche Funktion setDir($dir, $type = 'template') {
if(is_dir($dir)) {
if($type == 'template')
$this->templates_dir = rtrim($dir, '/').'/';
elseif($type == 'template_c')
$this->templates_c_dir = rtrim($dir, '/').'/';
elseif($type == 'cache')
$this->cache_dir = rtrim($dir, '/').'/';
anders
return false;
return true;
} anders {
return false;
}
}
öffentlicher Funktionscache($time) {
if(is_numeric($time)) {
$this->cache = $time;
return true;
} anders {
return false;
}
}
öffentliche Funktionzuweisung($var, $value = NULL) {
if (is_array($var)) {
foreach ($var as $key => $val) {
$this->t_vars[$key] = $val;
}
} anders {
$this->t_vars[$var] = $value;
}
}
private Funktion comp($filename) {
versuchen {
if(!$fp = fopen($filename, 'r')) {
throw new Exception('Kann nicht geöffnet werden ' . $filename);
}
$filesize = filesize($filename);
if($filesize <= 0) {
throw new Exception('Die Dateigröße muss > 0 ' );
}
$content = fread($fp, $filesize);
fclose($fp);
unset($fp);
$content = preg_replace("/<%=([$a-zA-Z0-9_]{1,})%>/","<?php echo \$$1 ;?>", $content);
$content = preg_replace("/<%([$a-zA-Z0-9_]{1,}).loop%>/", "<?php foreach( \$$1 as \$$1_key => \$$1_val ) { ?>",$content);
$content = preg_replace("/<%([$a-zA-Z0-9_]{1,}).loop(([$a-zA-Z0-9_]{1,})) %>/", "<?php foreach( \$$1 as \$$2 ) { ?>", $content);
$content = preg_replace("/<%([$a-zA-Z0-9_]{1,}).loop(([$a-zA-Z0-9_]{1,}),( [$a-zA-Z0-9_]{1,}))%>/", "<?php foreach( \$$1 as \$$2 => \$$3 ) { ?>", $content);
$content = preg_replace("/<%([$a-zA-Z0-9_]{1,}).key%>/", "<?php echo \$$1_key ;?>", $ Inhalt);
$content = preg_replace("/<%([$a-zA-Z0-9_]{1,}).value%>/", "<?php echo \$$1_val ;?>", $ Inhalt);
$content = preg_replace("/<%([$a-zA-Z0-9_]{1,})\?%>/", "<?php if( \$$1 == true) { ? >", $content);
$content = preg_replace("/<%end%>/","<?php } ?>", $content);
$content = preg_replace("/<%##common##%>([^<%##end##%>]{0,})<%##end##%>/", "<?php n/* $1 */n?>", $content);
if (preg_match_all("/<%{([^(}%>)]{1,})}%>/", $content, $files)) {
$this->comp($this->templates_dir . $files[1][0]);
}
$content = preg_replace("/<%{([^(}%>)]{1,})}%>/", "<?php include '{$this->templates_c_dir}simplet_comp_$1.php'; ? >", $content);
echo $content;
$fp = fopen($this->templates_c_dir . 'simplet_comp_' . basename($filename) . '.php', 'w');
if(!fwrite($fp, $content)) {
throw new Exception('Inhalt kann nicht in ' . $filename geschrieben werden);
}
fclose($fp);
} Catch (Ausnahme $e) {
echo $e->getMessage();
}
return true;
}
öffentliche Funktion display($filename) {
$filename = $this->templates_dir .
if(!file_exists($filename)) {
return false;
}
$t_filename_c = $this->templates_c_dir .'simplet_comp_' .'.php';
if(!file_exists($t_filename_c) || filemtime($t_filename_c) < filemtime($filename)) {
$this->comp($filename);
}
if($this->cache > 0) {
$cache_file = $this->cache_dir . Basisname($filename);
if(!file_exists($cache_file) || (time() - filemtime($cache_file)) > $this->cache) {
ob_start();
foreach ($this->t_vars as $key => $val) {
$$key = $val;
}
include($t_filename_c);
$content = ob_get_contents();
ob_end_clean();
$fp = fopen($cache_file, 'w');
fwrite($fp, $content);
fclose($fp);
echo $content;
unset($content);
} anders {
include($cache_file);
}
} anders {
foreach ($this->t_vars as $key => $val) {
$$key = $val;
}
include($t_filename_c);
}
}
}
?>
PHP-Code:
test.php
<?php
require_once('simplet.class.php');
$t = new SimpleT();
$t->cache(10);//Aktivieren Sie die Cache-Funktion und setzen Sie die Ablaufzeit auf 10 Sekunden
$t->assign('arrays', array(array('hello','world')));
$t->assign('condition',false);
$t->display('index.php');
?>
PHP-Code:
index.php (Vorlagendatei, im Ordner templates/ abgelegt)
<%##common##%>
Hier ist der Kommentar und unten ist die mehrdimensionale Array-Schleife
<%##end##%>
<%arrays.loop(value)%>
<%value.loop(name)%>
<%=name%>
<%end%>
<%end%>
<%##common##%>
Das Folgende ist das bedingte Urteil
<%##end##%>
<%condition?%>
<h1>Bedingung ist wahr</h1>
<%end%>
<%##common##%>
Unten ist die Include-Datei()
<%##end##%>
<%{filename.php}%>