> > On Aug 11, 2009, at 12:13 AM, hessiess@xxxxxxxxxxxx wrote: > >> Do *NOT* get into the habit of outputting your HTML using echo or >> print >> statements, it becomes unmaintainable very quickly, use a templating >> language, ether with a framework(recomended) or standalone. > > This sounds interesting. Could you expound on this a little more and > perhaps list a couple of the templates you mention? > > Thanks, > Frank > There are a number of options for templating in PHP such as smarty, Dwoo and PHP itself, though the syntax can be rather messy. Personally I just use a simple find and replace macro system to expand custom short-hand code into the more verbose PHP, then run it through exec and capture the result to a variable with output buffering, the class folows: <?php class view { var $str; /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * Load in template file and expand macros into PHP ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ function __CONSTRUCT($tplname) { $fh = fopen($tplname, 'r'); $this->str = fread($fh, filesize($tplname)); fclose($fh); $this->expand_macros(); } /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * Run the template and return a variable ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ public function parse_to_variable($array = array()) { extract($array); ob_start(); eval($this->str); $result = ob_get_contents(); ob_end_clean(); return $result; } /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * Expand macros into PHP ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ private function expand_macros() { // Expand if macro $this->str = str_replace("<if", "<?php if", $this->str); $this->str = str_replace("<eif~>", "<?php endif;?>", $this->str); // Expand loop macro $this->str = str_replace("<loop", "<?php foreach", $this->str); $this->str = str_replace("<eloop~>", "<?php endforeach;?>", $this->str); // Expand display macro $this->str = str_replace("<dsp", "<?php echo", $this->str); // Expand end tag macro $this->str = str_replace("~>", "?>", $this->str); // Add PHP close tag to exit PHP mode $this->str = "?>" . $this->str; } } ------------------------------------------------------------------------ This loads template files like the folowing: <form enctype="multipart/form-data" action="<dsp $upload_url ~>" method="post"> <p><input type="hidden" name="MAX_FILE_SIZE" value="90000000000" /></p> <p>Upload new file, max size <dsp $max ~>:</p> <p> <input name="uploaded_file" type="file" /> <input type="submit" value="Send File" /> </p> </form> <table> <tr> <th width="180px">Filename</th> <th width="60px">Link</th> <th width="90px">Size (KB)</th> <th width="50px">Delete</th> <tr> <loop ($files as $file): ~> <tr> <td><dsp $file['Name'] ~></td> <td><a href="<dsp $file['Path'] ~>">Link</a></td> <td><dsp $file['Size'] / 1000 ~></td> <td><a href="<dsp $file['d_url'] ~>">X</a></td> <tr> <eloop~> </table> --------------------------------------------------------------- And it can be used like this $dialogue = new view("template/file_display.tpl"); $dialogue = $dialogue -> parse_to_variable(array( 'upload_url' => $upload_url, 'max' => $max_size, 'files' => $files)); the $dialogue var now contains the compiled template, ready for displaying or integrating into another template. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php