Andreas Vogt: PHP Template

Beitrag lesen

Hallo und ein gutes neues Jahr!
Ich schreibe gerade an einem cms und bin momentan dabei mein Templatesystem zu überdenken.

Mein Template sieht z.B. so aus:
blocktemplate.tpl:

<tr>  
	<td valign="top">  
		<table border="0" width="100%" cellpadding="0" cellspacing="0">  
			<tr>  
				<td class="leftBlockHead" width="1%"><img src="template/images/lfbox.gif" /></td>  
				<td class="centerBlockHead" width="98%" style="height: 16px" valign="middle" nowrap="nowrap">{blockTitle}</td>  
				<td class="rightBlockHead" width="1%"><img src="template/images/rtbox.gif" /></td>  
			</tr>  
		</table>  
		<table border="0" width="100%" cellpadding="0" cellspacing="0">  
			<tr>  
				<td width="100%" class="blockborder" colspan="3">  
					<table width="100%" border="0" cellpadding="0" cellpadding="0">{blockContent}</table>  
				</td>  
			</tr>  
		</table>  
	</td>  
</tr>  
<tr><td class="blockSpacer"></td></tr>

Mein Template Loader:

  
class nkp_tpl {  
  
	var $loadedTemplates = Array();  
  
	function nkp_tpl($template){  
		// all templates wich will be loaded are stored in an array  
		foreach ($template as $tplName => $tplFile) {  
            $this->loadedTemplates[$tplName] = $this->tplContent($tplFile);  
        }  
    }  
  
	function handleSubstitutions($templateName, $templateSubstitutions){  
		// load the origin template file  
		$tpl = $this->loadedTemplates[$templateName];  
  
        // avoid injection of php or shell-code for security reason  
        $security_search  = array('#<\?php#i', '#\{$\{#', '#<\?#', '#<\%#', '#`#', '#<script[^>]+php#mi');  
        $security_pattern1 = "&lt;?php";  
        $security_pattern2 = "&lt;?";  
        $security_replace = array($security_pattern1, '', $security_pattern2, '', '', '' );  
  
        // do the template substitutions  
		foreach ($templateSubstitutions as $var => $subs) {  
            $subs = preg_replace($security_search, $security_replace, $subs);  
            $tpl = str_replace('{'.$var.'}', $subs, $tpl);  
        }  
  
		// print handled main template  
		print str_replace("\n\n", "\n", $tpl);  
		exit;  
	}  
	function loadModuleTpl($module, $tplname, $templateSubstitutions) {  
		if (file_exists("modules/$module/template/$tplname.tpl")) {  
			$tpl = file_get_contents("modules/$module/template/$tplname.tpl");  
			  
			// avoid injection of php or shell-code for security reason  
	        $security_search  = array('#<\?php#i', '#\{$\{#', '#<\?#', '#<\%#', '#`#', '#<script[^>]+php#mi');  
	        $security_pattern1 = "&lt;?php";  
	        $security_pattern2 = "&lt;?";  
	        $security_replace = array($security_pattern1, '', $security_pattern2, '', '', '' );  
			  
			foreach ($templateSubstitutions as $var => $subs) {  
				$subs = preg_replace($security_search, $security_replace, $subs);  
	           	$tpl = str_replace('{'.$var.'}', $subs, $tpl);  
	       	}  
			return($tpl);  
		}  
	}  
	function tplContent($file){  
		// check if origin template file exists and then return the content of file  
        if (file_exists($file)) {  
            return file_get_contents($file);  
        } else {  
             return '<p align="center"><span style="color:#FF0000;">Template Error Occurs:</span><br />Not able to open '.$file.'.</p>';  
        }  
	}  
}

Funktion loadModuleTpl wird aus den einzelnen Modulen aufgerufen. Es gibt noch weitere Funktionen für Blöcke etc.

Meine Frage ist nun ob das alles Mist ist, oder was sagt ihr dazu?
Eine Demo des Template findet ihr hier:
http://mkportal-support.de/nkportal/

Gruß Andreas