I'm working on my first php site, I'm running into an issue I can't see to figure out. I'm trying to have one php page that contains my structure, and others that inject their html inside, while retaining url changes so I can still direct link pages.
So far this is what I'm doing, but it doesn't seem efficient:
index.php
<html xmlns="http://www.w3.org/1999/xhtml">
    <?php include("head.php"); ?>
    <body>
        <div class="container">
            <!-- Navigation header -->
            <?php include("navigation.php"); ?>
            <!-- Main container -->
            <div id="MainContainer">
                <?php include("home.php"); ?>
            </div>
            <!-- Footer -->
            <?php include("footer.php"); ?>
        </div>
    </body>
</html>
about.php
<html xmlns="http://www.w3.org/1999/xhtml">
    <?php include("head.php"); ?>
    <body>
        <div class="container">
            <!-- Navigation header -->
            <?php include("navigation.php"); ?>
            <!-- Main container -->
            <div id="MainContainer">
                About me!
            </div>
            <!-- Footer -->
            <?php include("footer.php");  ?>
        </div>
    </body>
</html>
This feels totally wrong, if I ever want to change my container class, or change the structure, I now have to do it in two places instead of one.
In ASP.net MVC I would have a Layout_Head.cshtml file that would contain my HTML structure and inside I can render views from different pages, the url changes but the layout is always rendered first and then the controller/actions take care of injecting the html of the needed views.
How do I replicate that in PHP?
Usually people use php includes for templating more like this:
header.php
<html>
  <head>
    <title></title>
  </head>
  <body>
    <div class="container">
footer.php
    </div> <!-- .container -->
  </body>
</html>
about.php
<?php include('header.php'); ?>
  ... content goes here ...
<?php include('footer.php'); ?>
This is so you don't need to continuously repeat the start/end tags on every template you make.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With