HTML Templates -- php?

HTML Templates -- php?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

HTML Templates -- php?

You have a valid objection to copy-pasting boilerplate code across every file in your project. Repeating the <head>, navigation, and footer blocks in every .htm or .php file is an anti-pattern that violates the DRY (Don't Repeat Yourself) principle. While you could use server-side includes or JavaScript, using PHP to wrap content is the standard approach for dynamic sites, especially if you are already working with a server environment.

The core mechanism relies on template inheritance. You define a single layout file—let's call it layout.php—that contains your static HTML structure. This file includes a specific placeholder comment or variable where the dynamic content belongs. When you request a page, PHP reads the layout, locates that placeholder, and injects the content from your specific page file before sending the final HTML to the browser.

The Layout File

Create a new file, layout.php. This acts as your master template. It contains the full HTML skeleton, including the DOCTYPE, head tags, navigation, and footer. Crucially, inside the <article> tag where you want content to appear, insert a comment or a special marker.

<!DOCTYPE html>
<html>
<head>
    <title><?php echo isset($page_title) ? $page_title : '1914'; ?></title>
    <script src="modernizr-1.5.js"></script>
    <link href="styles.css" type="text/css" rel="stylesheet" />
    <meta charset="utf-8" />
</head>
<body>
  <div id="container">
    <header>
        <img src="images/banner.png" alt="World War I" style="border: none"/>
        <nav>
            <ul>
                <!-- Navigation links go here -->
                <li><a href="index.php">Home</a></li>
                <li><a href="1914.php">1914</a></li>
                <li><a href="1915.php">1915</a></li>
                <!-- Add other years -->
            </ul>
        </nav>
    </header>

    <section>
        <article class="content-placeholder">
            <?php echo $content; ?>
        </article>

        <aside>
            <!-- Sidebar content goes here -->
        </aside>
    </section>

    <footer style="font-weight: bold; letter-spacing: .1em">
        <a href="citations.php">Citations</a> &bull;
        <a href="about.php">About</a>
    </footer>
  </div>
</body>
</html>

Notice the <?php echo $content; ?> line. This is where the magic happens. The layout file expects a variable named $content to be passed to it.

Creating Page Content

Now, create your individual page files, such as 1914.php. These files should only contain the dynamic content. You do not repeat the header or footer here; you simply define the title and the article body, then wrap them in a helper function or include them into the layout.

Option A: Simple Include (No PHP logic inside the file)
If your page files are purely static HTML with no PHP tags of their own, you can use the include directive directly inside the main entry point (like an index.php) or create a small wrapper function. However, since your source material implies you want to mix content types, let's assume each page file might need its own logic.

Create a helper file, say content.php. This file handles the specific data for a year.

<?php
$page_title = "1914";
$content = <<<HTML
    <h1>The Year 1914</h1>
    <p>Enter your historical text here.</p>
HTML;
?>

The Entry Point

To make this work seamlessly, you need a central entry point. Create an index.php (or update your existing index.htm to .php) that loads the layout and injects the content.

<?php
// Define the current page's data
$page_title = "Home";
$content = <<<HTML
    <h1>Welcome to 1914</h1>
    <p>This is the main landing page.</p>
HTML;

// Load the layout and pass variables
include 'layout.php';
?>

When a user requests index.php, PHP executes the top block, sets $content, and passes it to layout.php. The echo $content in the layout prints the text inside the <article> tag.

Trade-offs and Pitfalls

Using this method introduces specific considerations:

  1. File Extensions: Your original code used .htm. If you switch to PHP, ensure your server is configured to serve .php files with HTML content types (text/html). Browsers handle .html fine, but they won't execute PHP logic inside them unless the file extension is recognized by the server configuration.
  2. Caching: If you are using a caching layer (like APCu or OPcache), changes to layout.php might take effect immediately, but old cached versions of individual page files might still serve stale data depending on your cache strategy.
  3. Security: Never trust the content passed into $content directly if it comes from user input without sanitization. In this specific school assignment context, you are likely hard-coding text, so XSS risks are minimal. However, if you later add dynamic features, ensure you escape output using htmlspecialchars() when generating HTML strings.
  4. Modern Alternatives: While this manual include approach works well for small projects or learning PHP, modern frameworks like Laravel handle templates differently using Blade syntax (@yield and @section). If you are building a larger application in the future, migrating to a framework is often worth the effort for better maintainability.

For your specific WWI assignment, creating layout.php and updating your files to *.php with a simple include block is the most direct solution that respects the DRY principle without requiring complex build tools.

Our network