Control Structures
Control Structures
Section titled “Control Structures”| Directive | Role |
|---|---|
@if / @elseif / @else / @endif |
Conditionals |
@unless / @endunless |
Inverted conditional |
@isset(expr) / @endisset |
Body when expr evaluates to not None |
@empty(expr) / @endempty |
Body when expr is empty / falsy |
@foreach(items as item) / @endforeach |
Loop with loop helpers |
@forelse / @empty / @endforelse |
Loop or empty state |
@for / @endfor |
Python for header |
@while / @endwhile |
While loop |
@auth / @endauth |
Body when auth_user or __authenticated is set |
@guest / @endguest |
Inverse of @auth |
@can / @cannot / @canany / @cannotany |
Gate / policy checks — see Authorization |
@python / @endpython |
Escape hatch |
Loop variable
Section titled “Loop variable”Inside @foreach / @forelse, loop exposes:
index, iteration, remaining, count, first, last, even, odd, depth, parent.
@foreach(users as user) <li @if(loop.first)class="first"@endif>{{ user.name }}</li>@endforeachBare @empty inside @forelse remains the empty branch. Standalone
@empty(expr) … @endempty is a separate empty-check directive.
Auth and authorization
Section titled “Auth and authorization”@auth <p>Welcome back</p>@endauth
@guest <a href="/login">Sign in</a>@endguest
@can('update', post) <a href="/edit">Edit</a>@endcan@can / @cannot / @canany call the authorization gate.
Localization in views
Section titled “Localization in views”@lang("messages.welcome", {"name": name}){{ __("messages.welcome", {"name": name}) }}__, trans, and trans_choice are injected into every template context.