Skip to content

Blade Templates

David Ciulla edited this page Nov 7, 2026 · 2 revisions

Blade Templates

Laravex ships a Blade-compatible template engine implemented in Laravex.View. Template files live in resources/views/ and use the .blade.php extension (.html and .eex are also resolved as fallbacks). The compiler runs entirely in Elixir with no PHP dependency.


Displaying Data

Escaped echo

{{ $title }}
{{ title }}

The $ prefix is optional. Output is HTML-escaped (&, <, >, " are encoded). Use dot-notation to access nested values:

{{ $user.name }}
{{ $post.author.email }}

Raw / unescaped echo

{!! $htmlContent !!}

Warning: Never echo untrusted user input unescaped.


Verbatim Blocks

Wrap any content you want the compiler to leave alone in @verbatim:

@verbatim
  Hello {{ this is not compiled }}
@endverbatim

Control Structures

If / elseif / else

@if($status == 'published')
  <span>Published</span>
@elseif($status == 'draft')
  <span>Draft</span>
@else
  <span>Unknown</span>
@endif

Unless

@unless($user)
  <a href="/login">Log in</a>
@endunless

Switch

@switch($status)
  @case('published')
    <span class="badge-green">Published</span>
    @break
  @case('draft')
    <span class="badge-gray">Draft</span>
    @break
  @default
    <span class="badge-red">Unknown</span>
@endswitch

Each @case is evaluated against the switch expression using ==. @default is used when no case matches. @break terminates the matched case block.

Isset and Empty

@isset($record)
  {{ $record.name }}
@endisset

@empty($items)
  <p>No items found.</p>
@endempty

@empty treats nil, "", [], and empty maps as blank.


Loops

foreach

@foreach($posts as $post)
  <h2>{{ $post.title }}</h2>
@endforeach

forelse

Renders the @empty branch when the list is empty:

@forelse($comments as $comment)
  <p>{{ $comment.body }}</p>
@empty
  <p>No comments yet.</p>
@endforelse

Layouts and Sections

Defining a layout

{{-- resources/views/layouts/app.blade.php --}}
<!DOCTYPE html>
<html>
<head>
  <title>@yield('title')</title>
  @stack('head')
</head>
<body>
  @yield('content')
</body>
</html>

Extending a layout

@extends('layouts.app')

@section('title')
  My Page
@endsection

@section('content')
  <h1>Welcome</h1>
@endsection

@extends must appear at the top of the file. Sections are injected into the layout's corresponding @yield placeholders.


Including Sub-views

{{-- Always include --}}
@include('partials.nav')

{{-- Include with extra data --}}
@include('partials.alert', ['type' => 'error', 'message' => $msg])

{{-- Include only if the file exists --}}
@includeIf('partials.sidebar')

{{-- Conditional includes --}}
@includeWhen($isAdmin, 'partials.admin-bar')
@includeUnless($isGuest, 'partials.user-menu')

Components

Defining a component

Place the template in resources/views/components/:

{{-- resources/views/components/alert.blade.php --}}
@props(['type' => 'info', 'title'])
<div class="alert alert-{{ type }}">
  @if($title)
    <strong>{{ title }}</strong>
  @endif
  {{ $slot }}
</div>

@props declares props with optional default values. Keys listed without a default are nil unless passed by the caller.

Using a component

@component('alert', ['type' => 'error', 'title' => 'Oops'])
  Something went wrong. Please try again.
@endcomponent

The main body is available as {{ $slot }} inside the component.

Named slots

@component('card')
  @slot('header')
    <h2>Card Title</h2>
  @endslot

  Card body content.
@endcomponent

Inside the component: {{ $header }} and {{ $slot }}.

@aware — inheriting parent data

@aware(['user'])

Pulls the listed keys from the parent component's assigns when they are absent in the current component.


Stacks

Stacks let child views push content (scripts, styles) into named slots in a layout.

{{-- In a child view --}}
@push('scripts')
  <script src="/app.js"></script>
@endpush

@prepend('scripts')
  <script src="/early.js"></script>
@endprepend

{{-- Push only once per page, even if the template is included multiple times --}}
@pushOnce('scripts')
  <script src="/chart.js"></script>
@endPushOnce

@prependOnce('scripts')
  <script src="/early-once.js"></script>
@endPrependOnce
{{-- In the layout --}}
@stack('scripts')

Forms

CSRF protection

<form method="POST" action="/posts">
  @csrf
  ...
</form>

@csrf renders <input type="hidden" name="_token" value="...">. The token value is also available as {{ csrf_token() }}.

HTTP method spoofing

HTML forms only support GET and POST. Use @method to spoof PUT, PATCH, or DELETE:

<form method="POST" action="/posts/1">
  @csrf
  @method('PUT')
  ...
</form>

Old input

After a failed validation redirect, the previous input is re-populated with old():

<input type="text" name="email" value="{{ old('email') }}">
<input type="text" name="name"  value="{{ old('name', $user.name) }}">

@selected, @checked, @disabled, @readonly, and @required

Conditionally add HTML boolean attributes:

<option value="admin" @selected($user.role == 'admin')>Admin</option>

<input type="checkbox" name="active" @checked($user.active)>

<input type="text" name="email" @disabled($locked)>

<input type="text" name="slug" @readonly($readOnly)>

<input type="email" name="email" @required($required)>

Each directive evaluates the expression and emits the attribute name when truthy. All support old() comparisons:

<option value="published" @selected(old('status') == 'published')>Published</option>

Validation Errors

@error('email')
  <p class="error">{{ $message }}</p>
@enderror

The $message variable contains the first error message for the field. errors is automatically shared with every view after a failed validation.


Flash / Session Messages

@session('status')
  <div class="alert">{{ $value }}</div>
@endsession

Renders only when a flash value with that key exists. The value is exposed as $value.


Authorization Directives

@auth
  <a href="/dashboard">Dashboard</a>
@endauth

@guest
  <a href="/login">Log in</a>
@endguest

@can('edit', $post)
  <a href="/posts/{{ $post.id }}/edit">Edit</a>
@endcan

@cannot('delete', $post)
  <span>Read-only</span>
@endcannot

@auth / @guest check whether a user is currently authenticated. @can / @cannot delegate to Laravex.Auth.Gate.allows?/3.


Once

Render a block only once per page, even if the containing template is included multiple times:

@once
  <script>window.__appBoot = true;</script>
@endonce

Conditional Classes

Build a class="..." attribute from a conditional map:

<div @class(['alert', 'alert-danger' => $errors, 'alert-info' => !$errors])>
  ...
</div>

Each entry that maps to a truthy condition is included. Bare string entries are always included.


Vite Assets

@vite(['resources/css/app.css', 'resources/js/app.js'])

During development (when public/hot exists) this emits HMR <script> and <link> tags pointing at the Vite dev server. In production it emits tags pointing at public/build/. See Asset-Bundling for details.


JSON Output

Encode and embed a PHP-safe JSON string directly in a template:

<script>
  window.user = @json($user);
</script>

Special HTML characters (<, >, &, line separator, paragraph separator) are Unicode-escaped so the value is safe to embed in a <script> block.


Pagination Links

@links($posts)

Renders HTML pagination links for a paginator result returned by Laravex.Http.Paginator.paginate/2.


Translations

@lang('messages.welcome')

{{ __('messages.welcome') }}

Both forms delegate to Laravex.Translation.get/1. See Localization for the full translation API.

Clone this wiki locally