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

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 

Anaconda update error PermissionError(13, 'Permission denied')

sanjay@luck:~$ conda update -n base conda Solving environment: done ## Package Plan ##   environment location: /home/sanjay/anaconda3   added / updated specs:     - conda The following packages will be UPDATED:     conda: 4.4.10-py36_0 --> 4.5.4-py36_0 Proceed ([y]/n)? y Preparing transaction: done Verifying transaction: done Executing transaction: failed ERROR conda.core.link:_execute(481): An error occurred while uninstalling package 'defaults::conda-4.4.10-py36_0'. PermissionError(13, 'Permission denied') Attempting to roll back. Rolling back transaction: done PermissionError(13, 'Permission denied') =========== you need to give a writable directory permission. on anaconda install directory. --> $ sudo chmod -R 755 anaconda3 sanjay@luck:~$ sudo chmod -R 777 anaconda3/  ============== sanjay@luck:~$ conda update anaconda-navigator Solving environment: done ## Package Plan ##   environment location: /home/sanjay/anaconda3   added / u