Cep Posted January 8, 2008 Share Posted January 8, 2008 Hello, I use a template class to create my html pages from PHP. This class will basically call up template files from a directory and then assign them to a PHP variable. To assemble the page I read in each template using the eval() construct, so that any PHP code in those templates is evaluated as PHP code in my script. These variables then appear in a "super" template and when the script is run I get a nice little html page The problem is I need to repeat several lines of code throughout my php scripts and I think it would be better if I created a function to do this but I am a bit unsure how because I am using eval(), which I do not believe returns a value in itself. These are the lines I repeat, eval("\$header = \"".$tpl->getTemplate("header_guest", "/headers")."\";"); eval("\$body = \"".$tpl->getTemplate("user_login", "/body/user")."\";"); eval("\$footer = \"".$tpl->getTemplate("footer_guest", "/footers")."\";"); // Output main template eval("\$tpl->outputTemplate(\"".$tpl->getTemplate("main")."\");"); The first three lines are sub templates, the variables $header, $body and $footer appear in the main template. The method getTemplate is the method in my template class which will read in a file with some HTML code in them. You don't need to see this class because its very basic and not important to my question so how can I repeat first three lines of code (as the main template is eval'd at the end of the script) using a function call? Quote Link to comment https://forums.phpfreaks.com/topic/85033-solved-how-can-i-reduce-the-repetition-of-this-code/ Share on other sites More sharing options...
trq Posted January 8, 2008 Share Posted January 8, 2008 how can I repeat first three lines of code (as the main template is eval'd at the end of the script) using a function call? If the $header, $body and $footer variables are needed within the main template you should pass them in as arguments to the getTemplate(0 method. Obviously your system is not designed to handle such things, but it would make your life alot easier. I would also refrain from using eval as much as possible. I'll try and show a simple example. tpl.class.php <?php class template { public function getTemplate($file,$args=array()) { if (file_exists($file.'.php')) { ob_start(); if (count($args)) { extract($args); } include $file.'.php'; return ob_end_clean(); } return false; } public function outputTemplate($out) { echo $out; } } ?> main.php <html> <head><title><?php echo $title; ?></title></head> <body> <p><?php echo $content; ?></p> </body> </html> index.php <?php include 'tpl.class.php'; $title = 'foo'; $content = 'bar'; $tpl = new template(); if ($out = $tpl->getTemplate('main',array($title,$content))) { $tpl->outputTemplate($out); } ?> I think Ive lost track of your question now, but what I'm getting at is if you pass the variables you need as arguments insstead of trying to have them fall into scope you will be able to wrap alot more of your code in functions, and therefor be able to wrap large pieces of repetative code together. Quote Link to comment https://forums.phpfreaks.com/topic/85033-solved-how-can-i-reduce-the-repetition-of-this-code/#findComment-433660 Share on other sites More sharing options...
Ken2k7 Posted January 8, 2008 Share Posted January 8, 2008 Shouldn't the index.php be: <?php include 'tpl.class.php'; $title = 'foo'; $content = 'bar'; $tpl = new template; if ($out = $tpl->getTemplate('main',array($title,$content))) { $tpl->outputTemplate($out); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/85033-solved-how-can-i-reduce-the-repetition-of-this-code/#findComment-433686 Share on other sites More sharing options...
trq Posted January 8, 2008 Share Posted January 8, 2008 Either is fine. Quote Link to comment https://forums.phpfreaks.com/topic/85033-solved-how-can-i-reduce-the-repetition-of-this-code/#findComment-433875 Share on other sites More sharing options...
Cep Posted January 9, 2008 Author Share Posted January 9, 2008 Thanks, will look into output buffering in the method [edit] Also just realised but I could just place the repeated code into include files as well Quote Link to comment https://forums.phpfreaks.com/topic/85033-solved-how-can-i-reduce-the-repetition-of-this-code/#findComment-434427 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.