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
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>
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 next line of code in Code Block 4 is our next line of PHP.



CODE BLOCK 4

<?php echo $pagemaincontent?>
11. 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

</body>
</html>
12. Code Block 5 is the rest of our file. This will end body and html sections.
13. In this particular case all we are keeping from page to page is the CSS files, and the basic HTML framework. To see a more advanced page, click here. The advanced version will handle all of your navigation, headers, and footers. The only thing you change from page to page is the main content. 14. Now to see how the content pages are created, click here.