Repository navigation
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.
{{ $title }}
{{ title }}The $ prefix is optional. Output is HTML-escaped (&, <, >, " are encoded). Use dot-notation to access nested values:
{{ $user.name }}
{{ $post.author.email }}{!! $htmlContent !!}Warning: Never echo untrusted user input unescaped.
Wrap any content you want the compiler to leave alone in @verbatim:
@verbatim
Hello {{ this is not compiled }}
@endverbatim@if($status == 'published')
<span>Published</span>
@elseif($status == 'draft')
<span>Draft</span>
@else
<span>Unknown</span>
@endif@unless($user)
<a href="/login">Log in</a>
@endunless@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>
@endswitchEach @case is evaluated against the switch expression using ==. @default is used when no case matches. @break terminates the matched case block.
@isset($record)
{{ $record.name }}
@endisset
@empty($items)
<p>No items found.</p>
@endempty@empty treats nil, "", [], and empty maps as blank.
@foreach($posts as $post)
<h2>{{ $post.title }}</h2>
@endforeachRenders the @empty branch when the list is empty:
@forelse($comments as $comment)
<p>{{ $comment.body }}</p>
@empty
<p>No comments yet.</p>
@endforelse{{-- resources/views/layouts/app.blade.php --}}
<!DOCTYPE html>
<html>
<head>
<title>@yield('title')</title>
@stack('head')
</head>
<body>
@yield('content')
</body>
</html>@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.
{{-- 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')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.
@component('alert', ['type' => 'error', 'title' => 'Oops'])
Something went wrong. Please try again.
@endcomponentThe main body is available as {{ $slot }} inside the component.
@component('card')
@slot('header')
<h2>Card Title</h2>
@endslot
Card body content.
@endcomponentInside the component: {{ $header }} and {{ $slot }}.
@aware(['user'])Pulls the listed keys from the parent component's assigns when they are absent in the current component.
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')<form method="POST" action="/posts">
@csrf
...
</form>@csrf renders <input type="hidden" name="_token" value="...">. The token value is also available as {{ csrf_token() }}.
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>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) }}">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>@error('email')
<p class="error">{{ $message }}</p>
@enderrorThe $message variable contains the first error message for the field. errors is automatically shared with every view after a failed validation.
@session('status')
<div class="alert">{{ $value }}</div>
@endsessionRenders only when a flash value with that key exists. The value is exposed as $value.
@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.
Render a block only once per page, even if the containing template is included multiple times:
@once
<script>window.__appBoot = true;</script>
@endonceBuild 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(['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.
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.
@links($posts)Renders HTML pagination links for a paginator result returned by Laravex.Http.Paginator.paginate/2.
@lang('messages.welcome')
{{ __('messages.welcome') }}Both forms delegate to Laravex.Translation.get/1. See Localization for the full translation API.
Laravex — Laravel-faithful application skeleton & runtime for Elixir.
GitHub
·
Docs
·
Limitations
·
Laravel Docs
Not affiliated with Laravel LLC or the Elixir core team. Built to feel like Laravel on the BEAM.
- Home
- Installation
- Configuration
- Directory-Structure
- Request-Lifecycle
- Service-Container
- Service-Providers
- Deployment
- Routing
- Middleware
- CSRF-Protection
- Controllers
- Requests
- Responses
- Views
- Blade-Templates
- Asset-Bundling
- Session
- Validation
- Error-Handling
- Logging
- Artisan-Console
- Broadcasting
- Cache
- Collections
- Events
- File-Storage
- Helpers
- HTTP-Client
- Localization
- Notifications
- Pipeline
- Queues
- Rate-Limiting
- Strings
- Task-Scheduling