Mario's Domain Logo
change style to old
Change style to new
Mario's Domain - ASP.NET 2.0 Master Pages in PHP Code/HowTo
Languages Used
XHTML
PHP
CSS
ASP.NET 2.0 has a nice feature called Master Pages. These are nice because you can define all of the framework for your page, navigation links, and everthign you want on all of your pages, in one file. The next step is to add the content on the individual pages, and link it to the master page.

Now the problem is, does your webserver handle ASP? In my case, no. I am on a Linux server running Apache 1, you need Apache 2 in order to handle ASP. So I decided I would learn PHP, and to my surprise, with a little manipulation, you can make master pages in PHP. I actually stole this idea from somewhere else, and didn't really pay attention where, so sorry for no credit.

1. The first step is to create any external files you want to link to, CSS, JavaScript, or whatever else.
2. Now you have all the framework from the CSS, or whatever you used for the site. Now you have to decide what elements of the page you will want on every page of the site. In my case I have everything except the main center column, or the main body, in the master page.
3. Start coding the master.php.




CODE BLOCK 1

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
4. The code in Code Block 1 is the start of an XHTML file. The first two lines are only needed if you want to validate the code, ideally you should. The third line only needs the html portion, but the rest is required for validation.
5. Next is to link to any external files, set up meta data, and add a title to the page. This is where your first line of PHP will come in to play.



CODE BLOCK 2

<head>
<meta name="revisit-after" content="30 days" />
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII" />
<title>Your Domain - <?php echo $pagetitle?> </title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
6. The first tag in Code Block 2head creates the area needed in order to link to any external files such as CSS in this case, give your page a title, and create meta data.
7. You see your first line of PHP code here in the line that starts with title. The ?php tells the server to run this as PHP, echo tells the server to write the contents of the following variable, $pagetitle to the browser window. We must remember the variable name for later, it is how we will link to the actual content.
8. Next we create the framework for the page. I am also using CSS, so there are many div tags here separating my page contents.



CODE BLOCK 3

<body>
<div id="pagewidth" >
<div id="headerleft" >
<!-- Place Holder for the header -->
</div><!-- headerleft end div -->
<div id="wrapper" class="clearfix" > 
<div id="twocols" class="clearfix">
<!-- Start the main content -->
9. Code Block 3 sets up the starting html tag body which is where all of what you see in a browser will appear.
10. The div tags all read from the CSS file for formatting the page.
11. The next line of code in Code Block 4 is our next line of PHP.



CODE BLOCK 4

<?php echo $pagemaincontent?>
12. This line is set up exactly like the other line of PHP code in the title. Only this time we call a different variable, $pagemaincontent. This variable will link to our actual content while the last variable was only to set up our page title.



CODE BLOCK 5
</div><!-- twocols end div -->
<div id="leftcol" >
<!-- Place holder for a navigation menu -->
</div><!-- leftcol end div -->
</div><!-- wrapper end div -->
<div id="footer" >
<!-- Place holder for a footer -->
</div><!-- footer end div -->
</div><!-- pagewidth end div -->
</body>
</html>
13. Code Block 5 is the rest of our file. This will end body and html sections as well as all of our div tags.This also sets up te left column for navigation, and a footer.
14. In this particular case we have design the whole page, minus the main content, and it will be reused from page to page. If we ever have to change links, or give a new logo in the header, all we make is one change and all of our pages are updated instantly.
15. Now to see how the content pages are created, click here.