Laravel Basics: Routing and Controllers
Blade Templates
2 min read
Last updated 46 minutes ago
Blade is Laravel's templating engine. It compiles to plain PHP and adds a clean, expressive syntax.
Echoing data
blade
<h1>{{ $post->title }}</h1>
{{ }} automatically escapes output — it's the safe default.
Layouts with yield
resources/views/layouts/app.blade.php:
blade
<!DOCTYPE html>
<html lang="en">
<head>
<title>@yield('title', 'My App')</title>
</head>
<body>
<header>@include('partials.nav')</header>
<main>
@yield('content')
</main>
</body>
</html>
A child view:
blade
@extends('layouts.app')
@section('title', 'Home')
@section('content')
<h1>Welcome home</h1>
@endsection
Directives
blade
@if ($user->isAdmin())
<p>Admin panel</p>
@elseif ($user->isSubscribed())
<p>Premium content</p>
@else
<p>Sign up for more</p>
@endif
@foreach ($posts as $post)
<article>{{ $post->title }}</article>
@endforeach
Components
Components are reusable UI pieces:
blade
{{-- resources/views/components/button.blade.php --}}
<a {{ $attributes->merge(['class' => 'btn']) }}>{{ $slot }}</a>
blade
<x-button href="/login" class="btn-primary">Sign in</x-button>
✓
Where to put them
Run php artisan make:component Alert to scaffold a class + view pair, or drop a .blade.php file directly into resources/views/components/.
Layouts vs components
- Layouts — the shared skeleton around your pages
- Components — reusable building blocks inside pages Use both together: layout for structure, components for repetition.
Advertisement