IThinkMyBrainHurts Posted April 7, 2015 Share Posted April 7, 2015 Hi, I'm writing a CMS, using a MVC pattern. I initially wrote the styling quickly, basically it takes a html file with some replaceable tags. Since then I've had a discussion about Wordpress and how that calls functions for the parts so that the page can process the information. This got me wondering if there are any standard approaches to the page output, templating, etc? Cheers Quote Link to comment https://forums.phpfreaks.com/topic/295344-page-output-and-templates/ Share on other sites More sharing options...
QuickOldCar Posted April 7, 2015 Share Posted April 7, 2015 Other options would be to integrate smarty or twig or at least look them over how they do it. If you are using php as your template engine you can include all through the index page and all styling would be there as your template. To clarify only your index page has the dividers with id's and class names, then include php scripts into those areas as needed. You could include header,navigation,content,footer,sidebar or optional not something just as easily. To expand upon that if wanted multiple themes to select from you could include specific ones. I've made a few cms using this approach and works out well. Creating a controller to include specific scripts depending on the navigation A simple example of the index page <?php //define $server_host = "http://" . $_SERVER['HTTP_HOST']; $document_root = $_SERVER['DOCUMENT_ROOT']; $directory_path = dirname(__FILE__) . DIRECTORY_SEPARATOR; $site_url = filter_var("http://" . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'], FILTER_SANITIZE_STRING); if (!empty($_SERVER['QUERY_STRING'])) { $query_string = filter_var($_SERVER['QUERY_STRING'], FILTER_SANITIZE_STRING); $site_url .= "?" . $query_string; } require_once($directory_path . "/db-connect.php"); require_once($directory_path . "/includes/session.php"); ?> <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width; initial-scale=1.0"> <title>Title</title> <META HTTP-EQUIV="Pragma" CONTENT="no-cache"> <META HTTP-EQUIV="Expires" CONTENT="-1"> <link href="style.css" rel="stylesheet" type="text/css"> <link href="media-queries.css" rel="stylesheet" type="text/css"> <!-- html5.js for IE less than 9 --> <!--[if lt IE 9]> <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <!-- css3-mediaqueries.js for IE less than 9 --> <!--[if lt IE 9]> <script src="http://css3-mediaqueries-js.googlecode.com/svn/trunk/css3-mediaqueries.js"></script> <![endif]--> </head> <body> <div id="background"> <img src="images/image.jpg" class="stretch" alt="background" /> </div> <div id="pagewrap"> <header id="header"> <hgroup> <?php require_once($directory_path . "/header.php"); ?> </hgroup> <nav> <?php require_once($directory_path . "/nav.php"); ?> </nav> <?php require_once($directory_path . "/search.php"); ?> </header> <div id="content"> <?php require_once($directory_path . "/content_controller.php"); ?> </div> <aside id="sidebar"> <?php require_once($directory_path . "/sidebar.php"); ?> </aside> <footer id="footer"> <?php require_once($directory_path . "/footer.php"); ?> </footer> </div> </body> </html> content_controller.php would determine which content to include depending on navigation/parameters in the url <?php if(!session_id()){ header("Location: http://".$_SERVER['HTTP_HOST']); exit; } /* actions and page controller parameter and page inclusion protection */ //only allow these GET paramters to pass in code $allowed_get = array( "action", "user", "id", "page" ); //loop get requests and remove any unwanted ones if (isset($_GET)) { foreach ($_GET as $key => $value) { if (!in_array($key, $allowed_get)) { unset($_GET[$key]); } } } //default page displayed if all else fails $page = "home.php"; /* loop through action array to determine destination $action_array located in actions.php, if action value is not in the array it won't load */ if (isset($_REQUEST['action'])) { if (in_array($_REQUEST['action'], $action_array)) { switch ($_REQUEST['action']) { case "home": //$page = "home.php"; header("Location: ".$server_host); exit; break; case "services": $page = "articles.php"; break; case "articles": $page = "articles.php"; break; case "help": $page = "articles.php"; break; case "help_post": $page = "help_post.php"; break; case "register": $page = "register.php"; break; case "account": $page = "account.php"; break; case "support": $page = "support.php"; break; case "users": $page = "users.php"; break; default: //$page = "home.php"; header("Location: ".$server_host); exit; } } } //include the page only if it exists $script = dirname(__FILE__) . DIRECTORY_SEPARATOR . $page; if (file_exists($script)) { require_once($script); } else { header("Location: ".$server_host); exit; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/295344-page-output-and-templates/#findComment-1508478 Share on other sites More sharing options...
IThinkMyBrainHurts Posted April 8, 2015 Author Share Posted April 8, 2015 Thankyou for your reply, I see where you're coming from, however you're not pulling the content from the Model, just including different prepared parts of the page. This is how far my notes / thinking extended: PAGE PRESENTATION ========================================================= METHOD 1 - Read in a file of HTML, string replace some tags, echo out. METHOD 2 - Finally include a HTML / PHP file which dumps HTML and uses PHP functions to retrieve data from the Model. *** In all cases here, the Model decides which page template to use. Also Model functions will be used to include other files, e.g. header, footer, etc. * NB the Model is a singleton class with function wrappers lol, don't slate the markup, this was my quick demo setup (but I LOVE TABLES!!!) Here's the old demo (string replace method): <html> <head> [INCLUDES] </head> <body> <table class="tBasic"> <tr><td colspan="2">[HEAD]</td></tr> <tr><td>[MENU]</td><td>[LOGIN]</td></tr> <tr><td colspan="2">[BODY_HEAD]</td></tr> <tr><td colspan="2">[BODY]</td></tr> <tr><td colspan="2">[FOOT]</td></tr> <tr><td colspan="2">[LOG]</td></tr> </table> </body> </html> And here's today's revision (page accesses the Model): <html> <head> <?php echo nub_block('INCLUDES'); ?> </head> <body> <table class="tBasic"> <tr><td colspan="2"><?php echo nub_block('HEAD'); ?></td></tr> <tr><td><?php echo nub_block('MENU'); ?></td><td><?php echo nub_block('LOGIN'); ?></td></tr> <tr><td colspan="2"><?php echo nub_block('BODY_HEAD'); ?></td></tr> <tr><td colspan="2"><?php echo nub_block('BODY'); ?></td></tr> <tr><td colspan="2"><?php echo nub_block('FOOT'); ?></td></tr> <tr><td colspan="2"><?php echo nub_block('LOG'); ?></td></tr> </table> </body> </html> This will still change though, e.g. the use of echo will soon not be required and some blocks will get passed an array (of posts, menu items, etc) Oh, I looked at Smarty and Twig last night. They are a step beyond what I'm after, I don't want to introduce another syntax to learn, I want to keep it simply HTML / CSS and a touch of PHP. Quote Link to comment https://forums.phpfreaks.com/topic/295344-page-output-and-templates/#findComment-1508497 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.