perchè mi piace :D
poi ho finito, mi manca solo una stupida funzione per gestire if e altri costrutti dal template :D
poi mi piace fare da me ;)
se uso le cose degli altri non impato mai, no?
e se magari un giorno per lavoro mi chiedevo un template engine ad-hoc? installo smarty? ( :2funny: )
lo faccio per passione :) mi piace troppo programmare quindi cerco sempre problemi che mi tengano impegnato :D
il template engine funziona : ( meno di 5 Kb di codice ) ma è ancora in debug :
<?php
/* solemareTemplateEngine */
/* provides caching, and template compiling */
/* TO DO :
* implements flux control structures into the templates
* template caching
* last compiled file ( $tplName ) use
*/
error_reporting(E_ALL);
class templateEngine{
/* template files extension */
private $tplExt = 'tpl';
/* directories */
private $sourcesDir = '../../templates/sources/';
private $compileDir= '../../templates/compiled/';
private $cacheDir= '../../templates/cache/';
/* variables delimiter */
private $varStart = '{{';
private $varEnd = '}}';
/* comments delimiter */
private $commentStart = '{[';
private $commentEnd = ']}';
/* template variables */
private $templateVars = array();
function __construct(){
}
function tplDisplay($tplName){
//{{{
/* tplDisplay
* @ params : $tplName : template file name ( including extension )
* @ license:
* @ version: 0.1 beta
* @return : void
*/
$chars = strlen('.' . $this->tplExt);
$tplNameCompiled = substr($tplName,0,-$chars);
/* remove file extension from $tplName; */
if(!$fileHash = $this->tplGetHash($tplName)){
/* if something went wrong with hash calculation */
echo 'Unable to calculate source hash for ' . $tplName;
}
if(!$this->is_tplCompiled($tplName, $fileHash)){
/* if the compiled sources does not exist */
$this->tplCompile($tplName, $tplNameCompiled, $fileHash);
/* compile the file */
}else{
//$fileHash = $this->tplGetHash($tplName);
if(!include($this->compileDir . $tplNameCompiled . '%' . $fileHash . '%' . '.php')){
echo '<strong>Engine error</strong> : unable to display <strong>' . $tplName . '</strong> !';
}
/* return the file */
}
}
function is_tplCompiled($tplName, $fileHash){
//{{{
/* is_tplCompiled
* @ params : $tplName : template file name ( including extension )
* $fileHash: template file md5 hash
* @ license:
* @ version: 0.1 beta
* @return : 1 file found / 0 file not found
*/
$chars = strlen('.' . $this->tplExt);
$fileName = substr($tplName,0,-$chars);
/* remove file extension from $tplName; */
if(!file_exists($this->compileDir . $fileName . '%' . $fileHash . '%' . '.php')){
/* if the compiled template file does not exitst */
return 0;
/* rerurn 0*/
}else{
/* the compiled source was found */
return 1;
/* return 1*/
}
//}}}
}
function tplGetHash($tplName){
//{{{
/* tplGetHash
* @ params : $tplName : template file name ( including extension )
* @ license:
* @ version: 0.1 beta
* @return : file hash / 0 file not found
*/
if(file_exists($this->sourcesDir . $tplName)){
$fileHash = md5_file($this->sourcesDir . $tplName);
/* create the md5 hash of the file to check if it's contents
* are diffetent */
return $fileHash;
/* return file hash */
}else{
/* the file does not exist */
return 0;
/* return 0 */
}
//}}}
}
function tplCompile($tplName, $tplNameCompiled, $fileHash){
//{{{
/* tplCompile
* @ params : $tplName : template file name ( including extension )
* : $tplNameCompiled : the compiled file name
* : $fileHash : source file hash
* @ license:
* @ version: 0.1 beta
* @return : void
*/
$file = $this->compileDir . $tplNameCompiled . '%' . $fileHash . '%' . '.php';
if($handle = opendir($this->compileDir)){
while(false !== ($fileList = readdir($handle))){
if($fileList !== '.' && $fileList !== '..'){
$tplNameParts = explode('%',$fileList);
if($this->tplGetHash($tplName) !== $tplNameParts[1]){
if(file_exists($this->compileDir . $tplNameCompiled . '%' . $tplNameParts[1] . '%' . '.php')){
unlink($this->compileDir . $tplNameCompiled . '%' . $tplNameParts[1] . '%' . '.php');
}
}else{
echo $fileList;
}
}
}
}
if(0){
unlink($file);
}
/* create the compiled file name */
if(!is_writeable($this->compileDir)){
/* if the compiled files folder is not writeable */
echo 'Engine Error : compiled sources directory is not writeable!';
}elseif($fileData = $this->tplParseSource($tplName)){
/* if the parser return the parsed file */
$handle = fopen($file,'wb+');
/* open the stream */
fwrite($handle,$fileData);
/* write the compiled source file */
if(!$handle){
/* if something went wrong during file save */
echo 'Compile Error : Error compiling <b>' . $tplName . '</b>';
}
fclose($handle);
/* close the stream */
}
//}}}
}
function tplParseSource($tplName){
//{{{
/* tplParseSource
* @ params : $tplName : template file name ( including extension )
* @ license:
* @ version: 0.1 beta
* @return : compiled file source / 0 cannot compile the file
*/
$error = false;
/* set the error to false */
$fileContent = file_get_contents($this->sourcesDir . $tplName);
/* get the source file contents */
$parsedContent = $fileContent;
/* set the file content into $parsedContent */
$tplPattern = '/[' . $this->varStart . '^]+(?:[a-z0-9_-])+[$' . $this->varEnd . ']+/';
/* this pattern define the VARIABLES matches */
if(preg_match_all($tplPattern,$parsedContent,$varName)){
/* check for matches */
foreach($varName[0] as $tplVar){
/* iterate the array */
$tplVarName = str_replace($this->varStart,'',$tplVar);
/* remove start tags from variable name */
$tplVarName = str_replace($this->varEnd,'',$tplVarName);
/* remove end tags from variable name */
$parsedContent = str_replace($tplVar,'<?php echo($this->templateVars[\'' . $tplVarName . '\']); ?>',$parsedContent);
/* replace the placeholder with the new variable */
}
}else{
echo 'Parse error : unable to parse the template file';
$error = true;
/* something went wrong */
}
if(!$error){
/* if the error is false */
return $parsedContent;
/* return the parsed file source */
}else{
return 0;
/* return 0 */
}
//}}}
}
function varAssign($tplVar, $userValue){
//{{{
/* varAssign
* @ params : $tplVar : the placeholder name {{placeholder}} into the template file
* : $userValue : the value that will be replaced with the placeholder
* @ license:
* @ version: 0.1 beta
* @return : void
*/
$this->templateVars[$tplVar] = $userValue;
//}}}
}
}
?>
ciao
ps : per la ruota : perchè comprare ( in questo caso è gratis, ma l'esempio vale ugualemente ) la ruota dalla FIAT , quando posso costrirla io stesso con le mie manine?
:bye: