I need to make some websites for work. I am new to it, so I am going pretty basic with bootstrap. There are a number of pages that will be identically structured, just with different text and links.
My question is, can I have a template php file that just reads a formatting txt file, or something along those lines? I am just hoping that I can manage one master file that keeps the structure consistent across all pages, but be able to tweak each page with a simple file that only changes what is needed.
I hope that makes sense. Please let me know if I need to clarify, and thank you in advance.
Advertisement
Answer
Use an includes page in your php structure. This page will have functions that create each structure of your pages that are the same. For example, you may have external links that are all the same that you use on each of your pages, you may also have a header and nav bar, maybe a sidebar and a footer that are all the same. On the includes.php
page you have functions that builds each of these components.
Following would be an example of a head section with a nav and single div section you may want to use on multiple pages you may also include a definitions page that defines different variables or defines that can be used in each page as well.
function evalPath($page){ switch ($page){ case "home": $path = ""; break; default: $path = "../"; } return $path; }; function constructHead($page, $metaDesc, $metaKeywords, $otherLinks, $siteroot){ $path = evalPath($page); $stmt = " <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="description" content="".$metaDesc.""> <meta name="keywords" content="".$metaKeywords.""> <meta name="author" content="".META['author'].""> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> ".$otherLinks." <link rel='stylesheet' type='text/css' href='$siteroot/css/main.css'> <link rel="icon" href="".$path."favicon.png"> <title>".$title."</title> </head> "; return $stmt; } function constructSingleDiv($spans, $content){ $stmt = " <div id='cont' class='container'> <div class='".$spans."'>". $content."</div><!--/END .'.$spans.' --> </div>"; return $stmt; } function constructNav($page, $siteroot){ //$page is the body id of the page calling the function and is defined on the page //example: ################################################## #### $page = 'home'; --> <body id='.$page.'> #### ################################################## $path = evalPath($page); $stmt = ' <div id="top-nav"><a class="adminLogin" href="'.$siteroot.'/admin/index.php">Login</a> <ul id="nav_bar" class="nav"> <li class="home"> <a href="'.$siteroot.'/index.php">Home</a> </li> <li class="media"> <a href="#media">Media</a> <ul> <li class="music"><a href="#music">Music</a> <ul> <li class=""><a href="#">Foil</a></li> <li class=""><a href="#">Long Drive home</a></li> <li class=""><a href="#">Sunday Girl</a></li> </ul> </li> <li class="video"><a href="'.$siteroot.'/pages/video.php">Video</a></li> <li class="art"><a href="'.$siteroot.'/pages/art.php">Art</a></li> <li class="pictures"><a href="'.$siteroot.'/pages/pictures.php">Pictures</a></li> <li class="web"><a href="'.$siteroot.'/pages/web.php">Web</a></li> </ul> </li><!-- END li.media --> <li class="contact"> <a href="'.$siteroot.'/pages/contact.php">Contact</a> </li> <li class="blog"> <a href="'.$siteroot.'/pages/blog.php">Blog</a> </li> </ul><!-- END ul.nav --> </div><!-- END div#top-nav -->'; //<p>current page is: '.$page.'</p> return $stmt; }
Now on my page where I wish to reuse this component of code I simply add an include function to include that file in the page then call on the function and pass the variables that add the content for each function I wish to use.
include 'includes.php';
Then to call my head section, I simply add the function…
constructHead($page, $metaDesc, $metaKeywords, $otherLinks, $siteroot);
Now above these functions that I call coming from my includes.php page, I need to define those variables I am passing into the functions… These variables are the content that is relevant to the page I am calling the function from the includes page on. The HTML or structure will be the same however the content will be defined on the page locally.
So on my home.php
I may define those variables as such…
// JUST below the opening php tag place the include statement include 'includes/functions.php'; $page = 'home'; $metaDesc = 'A few lines that describe what my page is about.'; $metaKeywords = 'code, coding, php, incudes, fucntions'; $otherLinks = 'scripts/js/index.js'; $siteroot = 'https://mypage.php'; $content = 'Something I want to say about my home page';
This example is very simple but you can see how this works.