📅 Week 14 - Day 2: Layouts & Partials

Tuesday | Duration: 3 hours | Build Reusable Template Structure! 🏗️

🎯 Learning Objectives

🏗️ What are Layouts?

Layouts are master templates that contain the common structure (header, footer, navigation) of your website. Child pages extend the layout and only define their specific content.

Benefits: Write once, use everywhere! Changes to header/footer automatically apply to all pages.

📐 Creating Master Layout

Step 1: Create app.blade.php Layout

<!-- resources/views/layouts/app.blade.php --> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>@yield('title', 'My Laravel App')</title> <style> * { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: Arial, sans-serif; } header { background: #fcb69f; color: white; padding: 20px; box-shadow: 0 2px 5px rgba(0,0,0,0.1); } nav { background: #333; padding: 0; } nav ul { list-style: none; display: flex; } nav a { color: white; text-decoration: none; padding: 15px 20px; display: block; } nav a:hover { background: #555; } .container { max-width: 1200px; margin: 0 auto; padding: 40px 20px; min-height: calc(100vh - 200px); } footer { background: #333; color: white; text-align: center; padding: 20px; margin-top: 50px; } @yield('styles') </style> </head> <body> <header> <h1>@yield('header', 'Laravel Website')</h1> </header> <nav> <ul> <li><a href="/">Home</a></li> <li><a href="/about">About</a></li> <li><a href="/services">Services</a></li> <li><a href="/contact">Contact</a></li> </ul> </nav> <div class="container"> @yield('content') </div> <footer> <p>&copy; 2025 My Laravel App. All rights reserved.</p> </footer> @yield('scripts') </body> </html>

👶 Creating Child Pages

Home Page (extends layout):

<!-- resources/views/home.blade.php --> @extends('layouts.app') @section('title', 'Home Page') @section('header', 'Welcome to Our Website') @section('content') <h2>Home Page Content</h2> <p>This is the home page content. It uses the master layout!</p> <div style="display: grid; grid-template-columns: repeat(3, 1fr); gap: 20px; margin-top: 30px;"> <div style="border: 1px solid #ddd; padding: 20px; border-radius: 8px;"> <h3>Feature 1</h3> <p>Amazing feature description here.</p> </div> <div style="border: 1px solid #ddd; padding: 20px; border-radius: 8px;"> <h3>Feature 2</h3> <p>Another great feature description.</p> </div> <div style="border: 1px solid #ddd; padding: 20px; border-radius: 8px;"> <h3>Feature 3</h3> <p>Third fantastic feature description.</p> </div> </div> @endsection

About Page:

<!-- resources/views/about.blade.php --> @extends('layouts.app') @section('title', 'About Us') @section('header', 'About Our Company') @section('content') <h2>Who We Are</h2> <p>We are a leading web development company specializing in Laravel applications.</p> <h3>Our Mission</h3> <p>To deliver high-quality web solutions that help businesses grow.</p> <h3>Our Team</h3> <ul> <li>Ahmad - Lead Developer</li> <li>Fatima - UI/UX Designer</li> <li>Ibrahim - Project Manager</li> </ul> @endsection @section('styles') <style> .content h2 { color: #fcb69f; margin-bottom: 15px; } .content h3 { color: #555; margin-top: 20px; } </style> @endsection

📦 Understanding Blade Directives

Directive Purpose Usage
@extends Extend a parent layout @extends('layouts.app')
@section/@endsection Define content section @section('content') ... @endsection
@yield Display section content @yield('content')
@include Include partial file @include('partials.header')
@parent Include parent section content @section('title') @parent - Page @endsection

🧩 Creating Partials

Partials are small, reusable view components like headers, footers, or navigation menus that can be included in multiple pages.

Header Partial:

<!-- resources/views/partials/header.blade.php --> <header style="background: linear-gradient(135deg, #fcb69f, #ffecd2); color: white; padding: 30px; text-align: center; border-radius: 10px; margin-bottom: 30px;"> <h1 style="font-size: 2.5em; margin-bottom: 10px;">{{ $title ?? 'Welcome' }}</h1> <p style="font-size: 1.2em;">{{ $subtitle ?? 'Subtitle here' }}</p> </header>

Navigation Partial:

<!-- resources/views/partials/nav.blade.php --> <nav style="background: #333; margin-bottom: 30px; border-radius: 5px;"> <ul style="list-style: none; display: flex; padding: 0; margin: 0;"> <li><a href="/" style="color: white; text-decoration: none; padding: 15px 20px; display: block;">Home</a></li> <li><a href="/about" style="color: white; text-decoration: none; padding: 15px 20px; display: block;">About</a></li> <li><a href="/services" style="color: white; text-decoration: none; padding: 15px 20px; display: block;">Services</a></li> <li><a href="/contact" style="color: white; text-decoration: none; padding: 15px 20px; display: block;">Contact</a></li> </ul> </nav>

Footer Partial:

<!-- resources/views/partials/footer.blade.php --> <footer style="background: #333; color: white; text-align: center; padding: 30px; margin-top: 50px; border-radius: 10px;"> <p>&copy; {{ date('Y') }} {{ $company ?? 'My Company' }}. All rights reserved.</p> <div style="margin-top: 15px;"> <a href="/privacy" style="color: #fcb69f; margin: 0 10px;">Privacy Policy</a> <a href="/terms" style="color: #fcb69f; margin: 0 10px;">Terms of Service</a> <a href="/contact" style="color: #fcb69f; margin: 0 10px;">Contact</a> </div> </footer>

🔌 Including Partials

Layout with Partials:

<!-- resources/views/layouts/main.blade.php --> <!DOCTYPE html> <html> <head> <title>@yield('title')</title> <style> body { font-family: Arial; max-width: 1200px; margin: 0 auto; padding: 20px; } </style> </head> <body> @include('partials.header', ['title' => 'My Website', 'subtitle' => 'Building with Laravel']) @include('partials.nav') <main> @yield('content') </main> @include('partials.footer', ['company' => 'Next-Step Technology']) </body> </html>

Page Using Layout with Partials:

<!-- resources/views/services.blade.php --> @extends('layouts.main') @section('title', 'Our Services') @section('content') <h2>Our Services</h2> <div style="display: grid; grid-template-columns: repeat(2, 1fr); gap: 30px; margin-top: 30px;"> <div style="border: 2px solid #fcb69f; padding: 30px; border-radius: 10px;"> <h3 style="color: #fcb69f;">Web Development</h3> <p>Custom web applications built with Laravel framework.</p> <ul> <li>Responsive design</li> <li>Database integration</li> <li>API development</li> </ul> </div> <div style="border: 2px solid #fcb69f; padding: 30px; border-radius: 10px;"> <h3 style="color: #fcb69f;">Mobile Apps</h3> <p>Native and cross-platform mobile applications.</p> <ul> <li>iOS & Android</li> <li>React Native</li> <li>Flutter development</li> </ul> </div> <div style="border: 2px solid #fcb69f; padding: 30px; border-radius: 10px;"> <h3 style="color: #fcb69f;">UI/UX Design</h3> <p>Beautiful and functional user interfaces.</p> <ul> <li>Wireframing</li> <li>Prototyping</li> <li>User testing</li> </ul> </div> <div style="border: 2px solid #fcb69f; padding: 30px; border-radius: 10px;"> <h3 style="color: #fcb69f;">Consulting</h3> <p>Expert advice on technology solutions.</p> <ul> <li>Architecture planning</li> <li>Code review</li> <li>Performance optimization</li> </ul> </div> </div> @endsection

💻 Complete Multi-Page Example

Controller:

<?php // app/Http/Controllers/PageController.php namespace App\Http\Controllers; class PageController extends Controller { public function home() { return view('home'); } public function about() { $team = [ ['name' => 'Ahmad', 'role' => 'Developer'], ['name' => 'Fatima', 'role' => 'Designer'], ['name' => 'Ibrahim', 'role' => 'Manager'], ]; return view('about', compact('team')); } public function services() { return view('services'); } public function contact() { return view('contact'); } } ?>

Routes:

<?php // routes/web.php use App\Http\Controllers\PageController; Route::get('/', [PageController::class, 'home']); Route::get('/about', [PageController::class, 'about']); Route::get('/services', [PageController::class, 'services']); Route::get('/contact', [PageController::class, 'contact']); ?>

✏️ Practice Exercises

  1. Create a master layout with header, navigation, content area, and footer
  2. Build 4 pages (Home, About, Services, Contact) extending the layout
  3. Create separate partials for header, nav, and footer
  4. Add custom styles to specific pages using @section('styles')
  5. Pass variables to partials using @include with parameters
  6. Create a blog layout with sidebar partial

✅ Today's Deliverable

← Previous Course Home Next →