sn2005 Posted December 14, 2012 Share Posted December 14, 2012 (edited) Greetings, I'm working on a small website with common features (login, members area, contact us, etc.) and using PHP to do the coding and MySQL as the database to store info. I've tried to separate the php code and all the functions and logic into php files and then have all the html code in separate files. I'll provide an example below of how I've done it so far. contact.php <?php session_start(); // If user is logged in, redirect them to the overview page if($_SESSION['ID']){ header('Location: main.php'); } include("classes/Output.class.php"); $page = new Page; // Output the HTML for the file $page->title = "Contact us"; $page->meta_keywords = "main overview stats"; $page->cssLink = "css/style.css"; $page->cssLinkIE = "css/ie.css"; $page->jsLink = array("js/jquery-1.8.3.js", "js/ajaxlogin.js"); $page->path = "./html/contact.html.php"; $page->displayPage($page->path); ?> So in my contact.php and other php files I call upon the Output class to output all of the data. Examples of the Output class are below Output.class.php <?php class Page { public $title; public $meta_keywords; public $cssLink; public $cssLinkIE; public $jsLink; public $body; public $path; public function displayHeader() { $output =' <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>'.$this->title.'</title> <meta name="keywords" content="'.$this->meta_keywords.'" /> <link rel="stylesheet" href="'.$this->cssLink.'" type="text/css">'; //<!--[if IE]><link rel="stylesheet" href="'.$this->cssLinkIE.'" type="text/css" media="all" /><![endif]--> for($i=0;$i<count($this->jsLink);$i++) { $output.= ' <script type="text/javascript" src="'.$this->jsLink[$i].'"></script>'; } $output.= ' </head> '; echo $output; } public function displayBody ($path, $var1=NULL, $var2=NULL) { include($path); } public function displayFooter () { $output = ' <div id="footer"> </div> </div> </div> </body> </html>'; echo $output; } public function displayPage($path, $var1=NULL, $var2=NULL) { $this->displayHeader(); $this->displayBody($path, $var1, $var2); $this->displayFooter(); } } ?> In my Output class, I have functions to output the header, main body and the footer. Then all these three functions are run within one main function named displayPage, which I call from my contact.php file: $page->displayPage($page->path); where the path is the file that contains the main body of the page, in this case the path refers to /html/contact.html.php. contact.html.php <body> <div id="wrapper"> <div id="container"> <?php include("./menu.login.php"); ?> <div id="content"> <img src="images/contactlogo.png" class="center"> <p1>If you have any problems please contact us at [email removed]</p1> <br/><br/> <form id="contact" method="post" action="contact.php"> <table> <tr> <td class="twocolright">Your Email</td> <td class="twocolleft"><input type="text" name="email" id="email" class="reginfo" /></td> </tr> <tr> <td class="twocolright">Subject</td> <td class="twocolleft"><input type="text" name="subject" id="subject" class="reginfo" /></td> </tr> <tr> <td class="twocolright">Message</td> <td class="twocolleft"><textarea name="msg" id="msg" class="msgbox" /></textarea></td> </tr> <tr> <td colspan="2"><input type="submit" name="msgbtn" id="msgbtn" value="" class="center" /></td> </tr> </table> </form> </div> So essentially what I'm trying to do is do all the coding/functions in the php and class files. Then call upon the Output class to handle the output of the page and store the core html body in a separate file/folder, away from the php code. What I'm curious about is, is the idea I'm using above a good way of separating the logic from the output and if you guys have any suggestions on what I could do to improve this or a different way of approaching this. Any insight would be appreciated. Thanks for your time Regards, SN2005 Edit: Some of the code is not being tabbed correctly, forgive any spacing/tab issues Edited December 14, 2012 by sn2005 Quote Link to comment https://forums.phpfreaks.com/topic/271984-separting-logic-from-design-ideas-for-outputting/ Share on other sites More sharing options...
freeloader Posted December 15, 2012 Share Posted December 15, 2012 Interesting question. I've been thinking of a clear way of doing it without using a pre-coded platform for some time as well. Looking forward to hearing an answer on this too. Quote Link to comment https://forums.phpfreaks.com/topic/271984-separting-logic-from-design-ideas-for-outputting/#findComment-1399599 Share on other sites More sharing options...
ignace Posted December 16, 2012 Share Posted December 16, 2012 (edited) All HTML should be in an HTML file, not in a PHP file/class. If you want to use a templating system you can achieve this using a two-step view. I highly suggest you check out Twig or Smarty instead of rolling your own. Edited December 16, 2012 by ignace Quote Link to comment https://forums.phpfreaks.com/topic/271984-separting-logic-from-design-ideas-for-outputting/#findComment-1399644 Share on other sites More sharing options...
cpd Posted December 22, 2012 Share Posted December 22, 2012 The MVC architecture is the theory behind what your trying to do. There are frameworks available such as CakePHP and CodeIgniter that use an MVC architecture and allow you to separate your logic. Quote Link to comment https://forums.phpfreaks.com/topic/271984-separting-logic-from-design-ideas-for-outputting/#findComment-1400906 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.