Witam, więc napisałem swoją pierwszą klasę do obsługi szablonów
Wiem, że nie warto bi istnieje smarty czy inne tego typu systemy ale napisałem ją dla własnego doświadczenie i teraz się nią dziele prosząc jednocześnie o opinie
<?php
class template {
private $compileDir;
private $templateDir;
private $variables = Array();
public function setCompileDir($dir) {
$src == '/' ? $this->compileDir = $dir : $this->compileDir = $dir.'/';
}
public function setTemplateDir($dir) {
$src == '/' ? $this->templateDir = $dir : $this->templateDir = $dir.'/';
}
public function assign($name, $value) {
$this->variables[$name] = $value;
}
public function assigns($value) {
foreach($value as $key => $result) {
self::assign($key, $result);
}
}
}
private function includeFile($file) {
self::display($file);
}
private function compile($tpl_file) {
$pattern[0] = '#{include file="(.*)"}#';
$pattern[1] = '#{if \$(.*) (==|!=|<|>|<=|>=) \$(.*)}#';
$pattern[2] = '#{if \$(.*) (==|!=|<|>|<=|>=) ([A-Za-z0-9\_-\s\"]+)}#';
$pattern[3] = '#{if !\$(.*)}#';
$pattern[4] = '#{if \$(.*)}#';
$pattern[5] = '#{elseif \$(.*) (==|!=|<|>|<=|>=) \$(.*)}#';
$pattern[6] = '#{elseif \$(.*) (==|!=|<|>|<=|>=) ([A-Za-z0-9\_-\s\"]+)}#';
$pattern[7] = '#{elseif !\$(.*)}#';
$pattern[8] = '#{elseif \$(.*)}#';
$pattern[9] = '#{else}#';
$pattern[10] = '#{/if}#';
$pattern[11] = '#{\$(.*)\.(.*)}#U';
$pattern[12] = '#{foreach \$(.*) as (.*)}#';
$pattern[13] = "#{/foreach}#";
$pattern[14] = '#{\$(.*)}#U';
$replacement[0] = "<?php self::includeFile('$1') ?>";
$replacement[1] = '<?php if($this->variables[\'$1\'] $2 $this->variables[\'$3\']) { ?>';
$replacement[2] = '<?php if($this->variables[\'$1\'] $2 $3) { ?>';
$replacement[3] = '<?php if(!$this->variables[\'$1\']) { ?>';
$replacement[4] = '<?php if($this->variables[\'$1\']) { ?>';
$replacement[5] = '<?php } elseif($this->variables[\'$1\'] $2 $this->variables[\'$3\']) { ?>';
$replacement[6] = '<?php } elseif($this->variables[\'$1\'] $2 $3) { ?>';
$replacement[7] = '<?php } elseif(!$this->variables[\'$1\']) { ?>';
$replacement[8] = '<?php } elseif($this->variables[\'$1\']) { ?>';
$replacement[9] = '<?php } else { ?>';
$replacement[10] = '<?php } ?>';
$replacement[11] = '<?php echo $$1[\'$2\'] ?>';
$replacement[12] = '<?php foreach($this->variables[\'$1\'] as $2) { ?>';
$replacement[13] = "<?php } ?>";
$replacement[14] = '<?php echo $this->variables[\'$1\'] ?>';
$header = '
<?php
$MD5_FILE_'.$name[0].' = "'.md5_file($this->templateDir.$tpl_file).'"; ?>
';
file_put_contents($this->compileDir.'template.'.$tpl_file.'.php',$header.$content);
require_once($this->compileDir.'template.'.$tpl_file.'.php');
} else {
throw new Exception('Cannot get information from tpl file');
}
}
public function display($tpl_file) {
throw new Exception('Template file does not found in '.$this->templateDir.$tpl_file);
}
if(file_exists($this->compileDir.'template.'.$tpl_file.'.php')) { require_once($this->compileDir.'template.'.$tpl_file.'.php');
$name = 'MD5_FILE_'.$name[0];
if($$name != md5_file($this->templateDir.$tpl_file)) { @unlink($this->compileDir.'template.'.$tpl_file.'.php'); self::compile($tpl_file);
}
} else {
self::compile($tpl_file);
}
}
}
?>
Obsługiwane tagi:{if
warunek}
{elseif
warunek2}
{/if}
{foreach
dane}
{/foreach}
{$zmienna.indeks} - tablica
{$zmienna} - zmienna
{include file="
nazwapliku"} - dołącza plik do szablonu