Blade basics

Echo with {{ }}

Blade templates live in resources/views and use the blade.php extension. Double curly braces print a variable, escaped so visitors cannot inject markup.

Whatever the controller passes to the view becomes a plain variable inside the template.

<h1>{{ $title }}</h1> <p>Hello, {{ $name }}!</p>

Loops

The foreach directive loops over arrays and collections with plain HTML inside. It reads like PHP but keeps the markup readable.

<ul> @foreach ($posts as $post) <li>{{ $post }}</li> @endforeach </ul>

Conditions

The if directive shows a block only when its condition holds. An optional else branch covers the empty state, like a list with no posts yet.

@if ($count > 0) <p>Showing {{ $count }} posts.</p> @else <p>Nothing here yet.</p> @endif

Layouts with @extends

Layouts hold the shared shell of every page, while child templates fill named sections. The yield directive marks the hole that children fill.

<html> <body> @yield("content") </body> </html>

A page adopts the layout with extends and fills the section with its own markup.

@extends("layouts.app") @section("content") <h1>About</h1> @endsection

Next: Blade advanced

Article author: Arthur Isaev

Banner image