[ pasvord @ 24.01.2007. 02:47 ] @
pozdravi :) Nasao sam na netu neki mali "template engine" i sve je radilo dok nisam iz baze ispisiva polje u kojem se nalazi html kod. Znaci imam $q_ = mysql_query("select * from ..."); $q = mysql_fetch_array($q_); echo $q['nesto']; Naravno, sve se fino ispise ali onda nece da {nesto} i {nestoDrugo} (to se nalazi u polju koje ispisujem) zamjeni sa vrijednostima koje sam zadao, dok kada napisem echo "{nesto} {nestoDrugo}"; (isto onako kako je u polju u bazi) normalno mijenja sa zadanim vrijednostima. template.engine.php (radi): Code: <? class TemplateEngine // This is our TemplateEngine class. { var $Template; // Current Templates Name. var $TemplateDir = './templates/'; // Templates Dir. function SelectTemplateFile($file, $error_line = 0, $error_file = '') // This function will select the requested templates .tpl file. { if(is_dir($this->TemplateDir)) // If the $templatedir exsits. { if(!file_exists($this->TemplateDir.$file.$this->TemplateExt)) // If the .tpl file does not exsit. { $error = "<b>File:</b> ".$error_file."<br/> <b>Line:</b> ".$error_line."<br/> <b>Date:</b> ".date("D M j G:i Y")."<br/><br/> Error loading ".$this->TemplateDir.$file.$this->TemplateExt.", file does not exist."; return die($error); // Returns $error. } elseif(file_exists($this->TemplateDir.$file.$this->TemplateExt)) // ElseIf the file exsits. { return $this->Template=file_get_contents($this->TemplateDir.$file.$this->TemplateExt); // Returns the current templates data. } // End ElseIf. } elseif(!is_dir($this->TemplateDir)) // ElseIf the directory does not exsit. { $error = "<b>File:</b> ".$error_file."<br/> <b>Line:</b> ".$error_line."<br/> <b>Date:</b> ".date("D M j G:i Y")."<br/><br/> Error opening ".$this->TemplateDir.", directory does not exist."; return die($error); // Returns $error. } // End ElseIf. } // End Function. function ReplaceVars($vars=array(), $error_line = 0, $error_file = '') // This will replace {var} with a array value. { if (sizeof($vars) > 0) // If the array of vars is not emtpy. { foreach ($vars as $var => $content) // Loops through the array. { $this->Template=str_replace("{".$var."}", $content, $this->Template); // Replaces {var} with the array value ($content). } // End Foreach. } else // Else the array of vars is empty. { $error = "<b>File:</b> ".$error_file."<br/> <b>Line:</b> ".$error_line."<br/> <b>Date:</b> ".date("D M j G:i Y")."<br/><br/> Error no tags destined for replacemnt."; return die($error); // Returns $error. } // End Else. } // End Function. function Compile() // This function will compile to template and display it. { eval("?>".$this->Template."<?"); // This simply allows us to add php into the .tpl file it self, and also returns the finished template. } // End Function. } // End Class. ?> index.php: Code: <? $host="localhost"; $pw=""; $un=""; $db=""; $sel = mysql_connect($host,$un,$pw) or die (mysql_error()); mysql_select_db($db,$sel); require_once("template.engine.php"); $TemplateEngine = new TemplateEngine; $TemplateEngine->SelectTemplateFile("template.php", __LINE__, __FILE__); $TemplateEngine->ReplaceVars(array( "name" => "NAME", "message" => "MESSAGE" ), __LINE__, __FILE__); $TemplateEngine->Compile(); ?> template.php - tpl file(ovdje nastaje problem :( ) file se nalazi u templates folderu: Code: <? $s_ = mysql_query("select * from tabelaz") or die (mysql_error()); $s = mysql_fetch_array($s_); $tpla = $s["izgled"]; echo $s["izgled"]; //Ispod radi, iznad ne $tpl = "<br><br> <html> <head> <meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1' /> <title>Template Engine Test</title> </head> <body> Hello {name}!<br> {message} </body> </html>"; echo $tpl; ?> [Ovu poruku je menjao pasvord dana 25.01.2007. u 03:00 GMT+1] |