Skip to main content

master page in php

Simple MasterPages with PHP
If you are an ASP.Net developer used to working with MasterPages (or any equivalant on another framework), you may be looking for an equivalant while working with php.  I was looking to do something like that with my homepage (hoolihan.net).  This is a very simple site, where a full framework would have been overkill.
Here’s how I pieced it out:
First: Create a template page, I called mine master.php.
<body>
<div><?php include('header.php');?></div>
<div><?php include('menu.php');?></div>
<div><?php include($page_content);?></div>
<div><?php include('footer.php');?></div>
</body>


Second: Create a content piece, for example about_text.php.
<p>This is information about me.</p>
Third: Create the page with the actual name you want to use:
<?php
$page_content = 'about_text.php';
include('master.php');
?>


<?php
$page_content = 'about_text.php';
include('master.php');
?>

save ->  about.php

One added benefit is that the content piece is then loadable in other places if need be.
Obviously, you’ll want to add a few bells and whistles.   For example, It’s a good idea to check for $page_content being null, and provide a default in that case.  I left out some of those things for the sake of brevity.
For more patterns in php, check out the following books:
PHP Objects, Patterns and Practice (Expert’s Voice in Open Source)
PHP Object-Oriented Solutions
Basically, it’s a real simple way to cleanly break up your php pages.  Hope you found  this useful…
Update: I’ve posted a follow-up article about the ajax ideas mentioned in this article. Also, see more php posts on this blog and more programming posts.
Update 2: User Ton posted a good comment below about how to setup the content variable (and simplify your url) with apache.

Comments

Popular posts from this blog

Connection Information To perform the requested action, WordPress needs to access your web server. Please enter your FTP credentials to proceed. If you do not remember your credentials, you should contact your web host.

Tech Easy Solutions Connection Information To perform the requested action, WordPress needs to access your web server. Please enter your FTP credentials to proceed. If you do not remember your credentials, you should contact your web host.  WordPress 5.2.3 hosted on Linux server  need file permission  sudo chown -Rf www-data.www-data  /www/html/worpdressdir change in wp-config

How to delete Torrent power account

Sanjay Tech solutions 3 easy tips to delete torrent power account  Step 1 : login in to your account Step 2 : click on more profile settings Step 3 : you see red button with deactivate account click then enter your password Your account deactivate 

Shopify fully custom contact section block on home page

Tech Easy Solutions Shopify fully custom contact section block on home page <section class="contact-information" >       <!--<div class="left-shape">         <img src="images/left-shape.png" class="img-fluid">       </div>-->       <div class="container c-container">         <div class="row">           <div class="col-lg-6 col-md-6 col-sm-12 col-12">             <div class=" contact-us">                         {% form 'contact' %}                       {% if form.posted_successfully? %}                        <center> <p class="note form-success">                   ...