Page 1 of 1

Injecting Content into a standard page layout

Posted: Sat Feb 20, 2010 11:31 am
by lpatterson
I am working on my first website/learning php. To this project I am dedicating a significant amount of time into the framework of my site. I am a huge fan of writing things once and never reproducing the code again within the same project. With that said I have run into a bit of a problem. Basically, I have designed multiple page layouts that have a location at which I would like to insert the content of my page (one of these layouts I have included below). I know that I have the layouts broken down such that they are not much code but I hate the idea of having to surround each individual page with these layouts. I want my pages to be more along the lines of the second small section of code posted (short and neat!). But I need a way of getting the content of the page into the location it belongs within the layout page. I'm thinking that I could (although I don't know how) write my layout as a function. Then I could read the content of my page and pass it to the function to build the page. Does anyone know how I can achieve this? I am also open to better solutions as I am just trying to learn to do things right here. Thanks!

Code: Select all

 
<?php
    include($baseDir."../../topFrame.php"); #Header
 
    echo "<table height=\"".$mainFrameHeight."\" border=\"0\" cellspacing=\"0\">"
 ?>
  <tr>
    <td width="170">
        <?php
            include($baseDir."../../menu.php"); #Menu
        ?>
    </td>
    <td>
        <!-- insert content of page in here -->
    </td>
  </tr>
</table>
<?php
    include($baseDir."../../bottomFrame.php"); #Footer
?>
 

Code: Select all

 
<?php
    $baseDir = "";  
    $mainFrameHeight = 680; 
?>
<!-- Content of the page-->
 

Re: Injecting Content into a standard page layout

Posted: Sat Feb 20, 2010 12:16 pm
by requinix
A few solutions. Get familiar with PHP and Apache and pick the one you like most.

The one I prefer is routing all requests through one file, such as index.php. This require_once's another file which loads your framework. It does whatever it wants to do (such as include a few other files), looks at the request, determines which file to serve, and includes it (then possibly includes more files).

Re: Injecting Content into a standard page layout

Posted: Sat Feb 20, 2010 12:28 pm
by jraede
To expand on that some, I route everything to index.php like he said. I then pass the URL to a rerouting function, which returns an array of information taken from the URL. Based on what's in that array, I require one of several initializing files to set all the correct variables, which then requires the page template that actually displays the information. That way you can have a folder called "templates" that has the three different page layouts you are using with <?php echo $content;?> or what have you where the content should go.