The Class (template.class.php) <? class Template { public $template; function load($filepath) { $this->template = file_get_contents($filepath); } function replace($var, $content) { $this->template = str_replace("#$var#", $content, $this->template); } function publish() { eval("?>".$this->template."<?"); } } ?> The Template File (design.html) <html> <head> <title>#title#</title> </head> <body> <h3>Hello #name#!</h3> <p>The time is: #datetime#</p> <? echo "<p>Embedded PHP works too!</p>"; ?> </body> </html> Usage (index.php) <? include "template.class.php"; $template = new Template; $template->load("design.html"); $template->replace("title", "My Template Class"); $template->replace("name", "William"); $template->replace("datetime", date("m/d/y")); $template->publish(); ?> Output code of index.php <html> <head> <title>My Template Class</title> </head> <body> <h3>Hello William!</h3> <p>The time is: 03/10/04</p> <p>Embedded PHP works too!</p> </body> </html> ===================================== This template system works ok. But I need few extra feature. Suppose, I add a variabe with no value to index.php $news = ""; then add the line to same file $template->replace("news", $news); Then add following lines to design.html #if news# #news# #endif news# (Like 4images template system) As a result I want nothing will display for #news# . Cause $news is blank. How it possible? (note: now for me its display #if news# #endif news# Please help me to solve this problem And also is this template system ok? And anything if required.
you need to write code to interpret it as php code...I wrote a template class once, resembled smarty actually...You should use smarty...if possible. otherwise you need to write code-blocks. preg_replace the #if (.*)# Hit me up on messenger...i'll send the template class i wrote.
Please look into a templating system called btemplate , that should help you add the basic constructs like Looping , conditional statements etc in your template system. Btemplate is a very simple system i had used long time back , look through its code to see the implementation of the construct required.
in index.php here load only one 1 template: design.html but I want to load 2 or more html template with the main template $template->load("design.html"); how it possible? ----------------------------------------------------- in index.php can i take following lines in a array. if yes, HOW? $template->load("design.html"); $template->replace("title", "My Template Class"); $template->replace("name", "William"); $template->replace("datetime", date("m/d/y")); suppose, $template->replace(( "title", "My Template Class", "name", "William")); or something like this.