PHP
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Functions
-
Section 4: Object-Oriented PHP
-
Section 5: Working with Data
-
Section 6: Modern PHP (PHP 8)
-
Section 7: Working with Files and Networking
-
Section 8: Common PHP Frameworks Overview
-
Section 9: Tooling and Ecosystem
-
Section 10: Practical Projects
-
Section 11: Interview Practice
-
Section 12: More Standard Library
-
Section 13: Data Structures and Algorithms in PHP
-
Section 14: More Practice Exercises
-
Section 15: More Security and Best Practices
-
Section 16: More Testing and Tooling
-
Section 17: WordPress-Style CMS Concepts
-
Section 18: Advanced OOP Practice
-
Section 19: More Web Fundamentals
-
Section 20: Database Practice
-
Section 21: PHP Manual: Array Functions
-
Section 22: PHP Manual: Date and Calendar Functions
-
Section 23: PHP Manual: Filesystem and Directory Functions
-
Section 24: PHP Manual: Filter and Var Handling
-
Section 25: PHP Manual: Math Functions
-
Section 26: PHP Manual: JSON and XML
-
Section 27: PHP Manual: Network and Stream Functions
-
Section 28: PHP Manual: Error and Exception Handling
-
Section 29: PHP Manual: Output Control and Misc
-
Section 30: PHP Manual: FTP, Zip, and Mail
-
Section 31: Modern PHP Frameworks Deep Dive
-
Section 32: PHP Design Patterns
-
Section 33: More Practice Exercises
-
Section 34: PHP Performance and Deployment
-
Section 35: More Interview Practice
-
Section 36: More PHP Standard Library
-
Section 37: PHP Concurrency and Async
-
Section 38: More Web Development Practice
-
Section 39: PHP Testing Deep Dive
-
Section 40: Composer and Package Development
-
Section 41: PHP Security Deep Dive
-
Section 42: More Practical Projects
-
Section 43: Legacy PHP Maintenance
-
Section 44: More Algorithm Practice
-
Section 45: Final Practice and Review
-
Section 46: PHP for E-Commerce Patterns
-
Section 47: PHP API Design Deep Dive
-
Section 48: PHP Caching Strategies
-
Section 49: PHP Queue and Background Jobs
-
Section 50: PHP Multi-Tenancy Patterns
-
Section 51: PHP Real-Time Features
-
Section 52: PHP CMS and Content Modeling
-
Section 53: PHP Internationalization
-
Section 54: More Framework-Specific Practice
-
Section 55: PHP Legacy Code Refactoring
-
Section 56: More Practice Projects Round 2
-
Section 57: PHP Command-Line Applications
-
Section 58: PHP and Microservices
-
Section 59: More Interview and Review Round 2
74: Templating Patterns for Content Sites
Imagine you're running a high-end hotel. Every guest gets a welcome folder in their room. That folder always has the same physical structure: a leather cover, a slot for the room key, a map of the city, and a list of hotel amenities. However, the specifics change. One guest sees "Welcome, Mr. Henderson" and a note about a vegan breakfast; another sees "Welcome, Ms. Chen" and a note about the spa.
You wouldn't build a brand new leather folder from scratch for every single guest. That would be insane. Instead, you have a "Master Template" (the folder) and "Dynamic Inserts" (the personalized letters).
In PHP, templating for content sites works exactly like that. We want to define the "leather folder" once and simply swap out the "inserts" based on which article or page the user is visiting.
The Blueprint vs. The Bricks
When I first started building content sites, I used to just copy-paste the header and footer into every single file. It worked until the client asked me to change a link in the navigation menu. I had to open 40 different files to change one word. I almost quit that day.
The professional way is to separate your Layout (the structural shell) from your Template (the specific page content). Here is how that looks in a real-world tech news site scenario. Instead of having about.php and contact.php as standalone files, we use a layout wrapper.
// layout.php
<?php
// We assume $pageTitle and $content are defined before this is called
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title><?php echo $pageTitle; ?> | TechPulse News</title>
<link rel="stylesheet" href="/css/style.css">
</head>
<body>
<header>
<nav><a href="/">Home <a href="/reviews">Reviews</nav>
</header>
<main>
<?php echo $content; ?>
</main>
<footer>
<p>© TechPulse</p>
</footer>
</body>
</html>
Capturing Content with Output Buffering
You might be wondering: "How do I get the HTML from my page file into that $content variable without writing a giant, ugly string?" This is where ob_start() comes in. It tells PHP: "Don't send the following output to the browser yet; just hold onto it in a buffer."
I use this pattern constantly because it keeps the logic clean. Look at how the article.php file now focuses only on the article itself, not the <html> tags.
// article.php
<?php
// 1. Fetch data from your database (simplified for this example)
$article = ['title' => 'The Rise of Rust', 'body' => '<p>Rust is gaining popularity...</p>'];
$pageTitle = $article['title'];
// 2. Start the buffer
ob_start();
?>
<article>
<h1><?php echo $article['title']; ?></h1>
<div class="content">
<?php echo $article['body']; ?>
</div>
</article>
<?php
// 3. Capture the buffer into a variable and clear it
$content = ob_get_clean();
// 4. Inject it into the master layout
include 'layout.php';
?>
Handling Reusable Chunks (Partials)
Sometimes a piece of content isn't a full page, but it's used in multiple places—like a "Trending Now" sidebar or a "Newsletter Signup" box. We call these Partials.
I recommend keeping these in a dedicated /partials directory. If your sidebar needs specific data, you can define those variables right before including the partial. It's a simple but effective way to keep your layout.php from becoming a 1,000-line monster.
// Inside layout.php, within the <main> tag:
<div class="container">
<div class="main-column">
<?php echo $content; ?>
</div>
<aside>
<?php
$sidebarTitle = "Hot Topics";
include 'partials/sidebar.php';
?>
</aside>
</div>
One quick tip: always use include or require for these templates, but be consistent. I personally use require for the layout because if the layout file is missing, the page is fundamentally broken and should throw a fatal error immediately rather than trying to render a half-baked page.
📋 Practical Task
Build a Dynamic Movie Review Template
Your goal is to create a templating system for a movie review site. You need to implement a master layout and a specific page for a movie review, ensuring that the review content is injected into the layout without duplicating the HTML boilerplate.
Requirements:
- Create a
layout.phpfile that contains the full HTML structure (head, body, nav, and footer). It must use a variable called$pageContentto render the main body. - Create a
movie-review.phpfile. In this file:- Define an associative array called
$moviecontaining a 'title', 'rating', and 'review_text'. - Use
ob_start()andob_get_clean()to capture the movie's HTML (including the title and rating) into the$pageContentvariable. - Include the
layout.phpfile at the end to render the final page.
- Define an associative array called
- Create a partial called
rating-star.phpthat simply echoes a star icon (★) or a specific piece of HTML. Include this partial inside yourmovie-review.phpbuffer to display the rating.
There are no comments for now.