• Home
  • About
  • Services
    • Technical SEO
    • Local SEO Services
  • Tools
    • Redirects Generator
    • HREFLANG Tag Generator
    • XML Sitemap Generator
    • XML Sitemap URL Extractor
    • Robots.txt Generator
    • Random URL Generator
    • Meta Robots Tag Generator
    • Organization Schema Generator
    • Article Schema Generator
    • Product Schema Generator
    • Local Business Schema Generator
    • FAQPage Schema Generator
    • WordPress Metabox Generator
    • Plugins & Scripts
  • Tutorials Hub
    • Technical SEO
    • WordPress
    • PHP
    • JavaScript
    • Python
    • Google Tag Manager
    • Shopify
    • Other Guides & Tutorials
    • General Discussion
    • View All
  • Contact
    • Hub
      New posts Search forums
    • What's new
      New posts New profile posts Latest activity
    • Members
      Current visitors New profile posts Search profile posts
    Log in Register
    What's new Search

    Search

    By:
    Advanced search…
    • New posts
    • Search forums
    Menu
    Log in

    Register

    Install the app
    • Home
      • Hub
      • PHP Scripts & Snippets
      You are using an out of date browser. It may not display this or other websites correctly.
      You should upgrade or use an alternative browser.

      Create a PHP template class for handling header and footer templates

      • Thread starter Chris
      • Start date May 1, 2023
      C

      Chris

      Administrator
      Staff member
      • May 1, 2023
      • #1
      Create a PHP template class for handling header and footer templates that will make your code more organised and maintainable.

      This approach is more flexible, and it allows you to add more methods and properties to your Template class as needed.

      Here's an example:

      PHP:
      class Template {
          private $documentRoot;
         
          public function __construct() {
              $this->documentRoot = $_SERVER['DOCUMENT_ROOT'];
          }
      
          public function header() {
              include $this->documentRoot . "/includes/header.php";
          }
      
          public function footer() {
              include $this->documentRoot . "/includes/footer.php";
          }
      }

      To use this class, you can instantiate it and call the methods like this:
      PHP:
      $template = new Template();
      
      $template->header();
      
      // Your page content goes here
      
      $template->footer();

      Previously, I had been using this approach:
      PHP:
      <?php
       // header
      function pf_Header() {
      
        include $_SERVER['DOCUMENT_ROOT']."/includes/header.php";
      
      }
      
       // footer
      function pf_Footer() {
      
        include $_SERVER['DOCUMENT_ROOT']."/includes/footer.php";
      
      }
      ?>
       
      You must log in or register to reply here.
      Share:
      Facebook Twitter Reddit Pinterest Tumblr WhatsApp Email Share Link

      New Posts

      • C
        add a number to the next column of data, increasing the number as the datapoint changes, and count how many times each datapoint appears
        • Started by Chris
        • Jul 20, 2023
        • Replies: 0
        Other Plugins & Tutorials
      • C
        How to connect to GPT-4 API with Node.js using PowerShell
        • Started by Chris
        • Jul 14, 2023
        • Replies: 0
        JavaScript Scripts & Snippets
      • C
        Google Search Results Scraper - Node JS & Puppeteer
        • Started by Chris
        • Jul 2, 2023
        • Replies: 0
        JavaScript Scripts & Snippets
      • C
        Detect unused JavaScript and CSS in Bulk
        • Started by Chris
        • Jun 29, 2023
        • Replies: 0
        JavaScript Scripts & Snippets
      • C
        Google Sheets Apps Script to Generate Hreflang tags
        • Started by Chris
        • Jun 12, 2023
        • Replies: 0
        Technical SEO
      • C
        Deploying Noindex Meta Robots Tag with JavaScript with Tag Manager - 2 Methods
        • Started by Chris
        • Jun 9, 2023
        • Replies: 0
        Google Tag Manager Tutorials & Guides
      • C
        Guide to Google Search Operators
        • Started by Chris
        • Jun 9, 2023
        • Replies: 0
        Other Plugins & Tutorials
      • C
        Bookmarklet to inspect and identify images that are not using lazy loading on a webpage
        • Started by Chris
        • Jun 2, 2023
        • Replies: 0
        JavaScript Scripts & Snippets
      • Hub
      • PHP Scripts & Snippets

    Technical SEO Consultant

    Chris Lever - Freelance SEO Consultant Availible for Hire. Specialist in Technical SEO Audits, Strategy and Implementation.

    Need Help?

    • BB Code
    • Cookie Policy
    • Terms of Use
    • Privacy Policy
    • HTML Sitemap

    Learn More

    • About
    • Availability
    • Popular Posts
    • Projects & Scripts
    • Contact

    Get in Touch

    Chris Lever, Manchester, United Kingdom
    hello@chrisleverseo.com
    +44 0161 883 7660

    © 2023 Chris Lever SEO. All rights reserved. Powered by Xenforo | PF

    • This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
      By continuing to use this site, you are consenting to our use of cookies.
      Accept Learn more…