note 75501 deleted from function.include by felipe

From: Date: Mon, 10 Dec 2007 18:26:35 +0000
Subject: note 75501 deleted from function.include by felipe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-133284@lists.php.net to get a copy of this message
Note Submitter: safak_ozpinar at NOSPAM dot yahoo dot com ---- This is a good example for returning a value from an included file I think. I'm working on my own template class like Smarty. This class gets html code from template files. Then it creates a file that will be included directly. It displays only installed modules for the page. The included page always checks the installed modules. If the comparision returns false, the included file returns FALSE and warns the class to recreate itself. Then a new file with requested modules will be overwritten to the existing one. Here is the main framework of my class: <?php /* by Safak Ozpinar */ class MyInclude { private $modules = array(); private $numModules = 0; /* the constructor gets module names into an array */ function MyInclude($inputModules) { if(!is_array($inputModules)) $inputModules = array($inputModules); foreach($inputModules as $module) { $this->modules[$this->numModules] = $module; $this->numModules++; } } /* the main action happens here, check carefully */ function Display($fname, $reCreate = FALSE) { if(@file_exists($fname) && !$reCreate) { // if the included file returns FALSE, recreate it if((include $fname)==FALSE) { $this->Display($fname, TRUE); return; } } else { $this->Create($fname); include($fname); } } /* the behavior of the method Create depends on the format of your template file */ // you will need some changes on this method to adapt to your work private function Create($fname) { $contentsPHP = '<?'."\n"; $contentsHTML = '<html><body>'."\n"; foreach($this->modules as $module) { $contentsPHP .= '$storedModules[] = \''.$module.'\';'."\n"; // the html code of the module (you may need to take from a tempate file) $contentsHTML .= '<p>HTML code for module <b>"'.$module.'"</b></p>'."\n"; } // here is the comparision code for included file // if the comparision returns FALSE, the file also returns FALSE $contentsPHP .= 'if(!$this->Compare($storedModules)) return FALSE;'."\n"; $contentsPHP .= '?>'."\n"; $contentsHTML .= '</body></html>'; $fp = fopen($fname,'wb'); fwrite($fp, $contentsPHP.$contentsHTML); fclose($fp); } /* ordered comparision, you may want to change the operations in this method */ function Compare($storedModules) { $count = count($storedModules); if($count == $this->numModules) { for($i=0; $i<$count; $i++) { if(strcmp($storedModules[$i],$this->modules[$i])!=0) return FALSE; } return TRUE; } else return FALSE; } } /* lets go */ $tpl = new MyInclude(array('headline','main_menu','sub_menu','text_ads')); $tpl->Display('inc.php'); ?> I haven't written error controls here because the code would be too large. For instance; if a html template for requested modules doesn't exist, you may want to display an error message or make some operations. --- Safak Ozpinar (from Istanbul University)

« previous php.notes (#133284) next »