Page 1 of 1

Problem implementing Master pages

Posted: Thu Jul 24, 2008 4:12 pm
by jhall
So I read this article on using the idea of ASP.NET Master Pages in PHP:

http://spinningtheweb.blogspot.com/2006 ... n-php.html

And I'm trying to implement it. I've basically gotten everything to work, but there's one major issue occurring. I'm not sure I'm doing this all correctly (I'm pretty new to PHP), so it might be an obvious thing. Here's my code: (I've bolded the important stuff in master.php)

master.php:

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 
<html dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
 
<head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
    <meta name="verify-v1" content="8nCOczUjgwuafd6sbyKUnGHVe2Gmh4Bx08ELDAFbeY0=" />
    <link rel="shortcut icon" href="http://www.intelligentfuture.org/favicon.ico" />
    <link href="IF.css" rel="stylesheet" type="text/css" />
    <!--[if lt IE 7]>
    <link href="IFIE6.css" rel="stylesheet" type="text/css" /> 
    <![endif]-->
    <script type="text/javascript" src="scripts/jquery-1.2.6.js" ></script>
    <script type="text/javascript" src="scripts/extend.js" ></script>
    <script type="text/javascript" src="scripts/thickbox.js"></script>
    <link rel="stylesheet" href="scripts/thickbox.css" type="text/css" media="screen" />
    <title>[b]<?php echo $pagetitle; ?>[/b]</title>
    [b]<?php echo $Head; ?>[/b]
    
</head>
 
<body>
        <div id="pageWrap">
        
            <div id="header">
                <h1>Intelligent Future</h1>
                <ul id="menu">
                        [b]<?php echo $Menu; ?>[/b]
                </ul>   
            </div>
            
        <div id="beingUpdated">This site is being updated</div>
        
        <div id="main">
            [b]<?php echo $MainContent; ?>[/b]
        </div>
        
        <div id="footer">
            <div id="footerLinks">
                <ul>
                    <!--<li>
                        <a class="reveal" href="#">Join the Project</a>
                        <span class="footerMessage">Sign on to the Peace on Demand campaign; receive updates and event announcements.</span>
                    </li>-->
                    <li>
                        <a class="reveal" href="mailto:angelgaret@gmail.com?subject=Support%20the%20Project">Support the Project</a>
                        <span class="footerMessage">Help fund Intelligent Future by mail or PayPal.</span>
                    </li>
                    <li>
                        <a class="reveal" href="mailto:angelgaret@gmail.com?subject=Request%20Event">Request Event</a>
                        <span class="footerMessage">Arrange a PeaceOnDemand concert (comedy/music) or speaking engagement</span>
                    </li>
                </ul>
            </div>
            <div id="footerMenu">
                <ul>
                    [b]<?php echo $FooterMenu; ?>[/b]
                </ul>
            </div>
 
            <div id="copy">&copy; 2008 Intelligent Future</div>
        </div>
        
        </div>
        
        <div id="valid">
 
            <a href="http://validator.w3.org/check?uri=referer">
                <img src="http://www.w3.org/Icons/valid-xhtml10-blue" alt="Valid XHTML 1.0 Strict" height="31" width="88" />
            </a>    
 
            <a href="http://jigsaw.w3.org/css-validator/">
                <img style="border:0;width:88px;height:31px"
                src="http://jigsaw.w3.org/css-validator/images/vcss-blue"
                alt="Valid CSS!" />
            </a>
 
        </div>
</body>
 
</html>
default.php:

Code: Select all

<?php ob_start(); //Start the buffer
?>
    <meta name="description" content="The homepage for Intelligent Future, Nik Peace - official observer of human civilization, and Brian Bogart - defense policy analyst and the nation's first Master's degree candidate in Peace Sciences." />
    <meta name="keywords" content="intelligent future,nik peace,brian bogart,defense industry,peace sciences,diversity,department of defense,Pentagon"/>
<?php
  $Head = ob_get_contents(); //Set $Head equal to buffer contents
  ob_clean();                //Clean the buffer
?>
    <li class="here">Home</li>
    <li><a href="nikPeace.php">Nik Peace</a></li>
    <li><a href="keyInfo.php">Key Info</a></li>
    <li><a href="essays.php">Essays</a></li>
    <li><a href="aboutBrian.php">About Brian</a></li>
    <li><a href="helpUs.php">Help Us</a></li>
    <li><a href="contact.php">Contact</a></li>
<?php
  $Menu = ob_get_contents(); //Set $Menu equal to buffer contents
  ob_clean();                //Clean the buffer
?>
<div id="home" class="center">
 
    <p>Lorem Ipsum... bunch of irrelevant stuff</p>
 
</div>
<?php 
 $MainContent = ob_get_contents(); //Set $MainContent equal to buffer contents
 ob_clean();                       //Clean the buffer
?>
    <li class="here">Home</li><li>|</li>
    <li><a href="nikPeace.php">About Nik</a></li><li>|</li>
    <li><a href="keyInfo.php">Key Info</a></li><li>|</li>
    <li><a href="essays.php">Essays</a></li><li>|</li>
    <li><a href="aboutBrian.php">About Brian</a></li><li>|</li>
    <li><a href="helpUs.php">Help Us</a></li><li>|</li>
    <li><a href="contact.php">Contact</a></li>
<?php
  $FooterMenu = ob_get_contents();  //Set $FooterMenu equal to buffer contents
  ob_end_clean();                   //Clean and stop the buffer
  $pagetitle = "Intelligent Future - One Person Does Make a Difference"; //Set the page title
  include("master.php");            //Apply the template
?>
When the page is viewed, there is a gap at the top of the page, caused by about half of the <head> elements being put in the <body>, along with a mysterious empty line. I can't figure out why this is happening. I thought the conditional statement for the IE6 stylesheet might be the culprit, but removing it has no effect. When viewed in firebug, this is how the page is being rendered:

Code: Select all

 
<head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <meta content="8nCOczUjgwuafd6sbyKUnGHVe2Gmh4Bx08ELDAFbeY0=" name="verify-v1"/>
        <title>Intelligent Future - One Person Does Make a Difference</title>
        <meta content="The homepage for Intelligent Future, Nik Peace - official observer of human civilization, and Brian     Bogart - defense policy analyst and the nation's first Master's degree candidate in Peace Sciences." name="description"/>
        <meta content="intelligent future,nik peace,brian bogart,defense industry,peace sciences,diversity,department of defense,Pentagon" name="keywords"/>
</head>
<body>
 
     <link href="http://www.intelligentfuture.org/favicon.ico" rel="shortcut icon"/>
     <link type="text/css" rel="stylesheet" href="IF.css"/>
     <script src="scripts/jquery-1.2.6.js" type="text/javascript">
     </script>
     <script src="scripts/extend.js" type="text/javascript">
     </script>
     <div id="pageWrap">
     ... the rest of the site (working correctly)
 
The really weird thing is that when you view the page source, it all looks normal!

Does anyone know why these elements are being thrown into the <body> section instead of staying where they should?? I've been banging my head against a wall for hours, so any help would be much appreciated!!

Re: Problem implementing Master pages

Posted: Fri Jul 25, 2008 12:44 pm
by ghurtado
Sounds like a problem with the output buffering functions in PHP. Just to troubleshoot, try changing ob_clean to ob_end_clean, and restart output buffering with every template block using ob_start() every time.

Just a thought to see if that would work.

Re: Problem implementing Master pages

Posted: Fri Jul 25, 2008 12:53 pm
by jhall
Actually, that's how I was doing it originally. Didn't work that way either. :(