Nitro

Essentials

Views

A component's view is a Blade view. Nitro compiles it once, and the server and the browser render that one compiled view: the server for the first page, the browser whenever the state changes, updating only what changed.

On this page

You write a component's view like any other Blade view, with echoes, @if, @foreach, @class, Laravel's helpers and PHP's functions. Because the view also runs in the browser, it can only use what works in both places. When something can't run in the browser, the build tells you, with the file and line.

A view rendering in the browser live, in this page View source ↓
  • Ada Lovelace · Mathematician •••••••• 0018
  • Grace Hopper · Rear Admiral •••••••• 0342
  • Alan Turing · Computer Scientist •••••••• 0571
  • Katherine Johnson · Space Scientist •••••••• 0923

4 of 4 contacts

The component

When you type, the list is filtered in the browser. @forelse, $loop->first, @class and the Str:: helpers all run there, and they give the same results as on the server.

Browser + server
<?php

namespace App\Nitro\Components;

use Nitro\Attributes\Computed;
use Nitro\Component;

class Contacts extends Component
{
    public string $search = '';

    public array $contacts = [
        ['name' => 'ada lovelace', 'role' => 'mathematician', 'phone' => '020 7946 0018'],
        ['name' => 'grace hopper', 'role' => 'rear admiral', 'phone' => '020 7946 0342'],
        ['name' => 'alan turing', 'role' => 'computer scientist', 'phone' => '020 7946 0571'],
        ['name' => 'katherine johnson', 'role' => 'space scientist', 'phone' => '020 7946 0923'],
    ];

    #[Computed]
    public function found(): array
    {
        $search = strtolower(trim($this->search));

        return array_values(array_filter($this->contacts, fn ($contact) => $search === '' || str_contains($contact['name'], $search)));
    }
}

One root element

A view must have a single root element, which is the component's own element. Everything the view renders goes inside it.

What you can use in a view

In a view
Echoes {{ }} , {!! !!}
Control flow @if @elseif @else @unless @isset @empty @foreach (with $loop ) @forelse @for @while @break @continue @switch
Attributes @class @style @checked @selected @disabled @readonly @required
Forms @error and $errors , @csrf , @method
Data @json
Children Components by their tag, with :key in a loop
Dynamic children nitro:dynamic-component with :is
Slots nitro:slot , printed as $slot and $slots['name']
Teleport @teleport('modals') ... @endteleport , rendered at @portal('modals') in the layout
Deferred @deferred('stats') ... @placeholder ... @enddeferred in a page (see Page data)
Script @nitroScript ... @endNitroScript : JavaScript run once for each instance as it goes live, never on the server (see Third-party libraries)
Laravel helpers route() , url() , asset() , __() / trans() , and config() for the keys in nitro.public_config
PHP Operators, casts and PHP's functions: strings, mbstring, arrays, math, ctype, URLs, HTML entities, preg_* , sprintf , json_encode and more, tested against PHP
Str:: 54 of Laravel's string helpers ( slug , limit , title , headline , camel , snake , mask , squish , ...), tested against Laravel

Nitro inlines @include when it compiles the view, and compiles each anonymous Blade component (<x-panel>) once for the attributes and slots its tags give. Anything else, such as @php, class-based Blade components or custom directives, can't run in the browser, and the build fails with the file and line.

Where it differs from Laravel's Blade

Both of Nitro's renders are tested against Laravel's Blade, feature by feature, in every place a view can hold it. Where Nitro can't render something as Blade would, the build fails with the file, the line and what to write instead:

  • @php, custom directives, class-based Blade components, @aware, <x-dynamic-component> (use nitro:dynamic-component), and a slot whose name is worked out as the page runs.
  • A loop's variable read after an @if (or another loop) that the loop is in has ended, because the block may not have run. Right after the loop, it has its last value, as in PHP.
  • An element opened in @if or @foreach and closed outside it, and markup the browser would rebuild, such as a <div> inside a <p>, a row inside a row, or markup inside an <option>, also when a Blade component puts it there. Table tags HTML lets you leave out (<tbody>, <tr>, <colgroup>) are added as the browser adds them, except around rows that a loop renders directly in a <table>: write the <tbody> there.
  • A Blade component that reads data the views share (View::share()) without being given it. Pass it as an attribute.

A few things render, but not quite as Laravel would:

  • Data shared while a request runs, by a view composer or a middleware, doesn't reach an @included view or a Blade component, because they're compiled into the view. Pass what they need as data or attributes.
  • @includeFirst picks its view when the view is compiled.
  • The server writes its HTML from the compiled view. The page the browser builds from it is the same as from Blade's HTML, but the bytes can differ, for example attributes in double quotes. In tests, check what a component shows (assertSee), not Blade's exact HTML.

Displaying data

{{ $name }} escapes its value, in the browser as on the server. {!! $html !!} doesn't escape it, so use it only for HTML you trust. To read a computed value, use $this->total.

Rendering child components

A view can place other components by their tag and pass data to their mount() method (see Components). Inside a loop, give each one a :key. To render a component whose name comes from a value, use nitro:dynamic-component:

Blade
<nitro:dynamic-component :is="$tab" />   {{-- 'settings.profile', 'settings.billing', or '' to render nothing --}}

Using slots

A component can wrap content that its parent passes in. It can receive a default slot and any number of named slots.

<nitro:card>
    <p>{{ $post['title'] }} has {{ $post['comments'] }} comments.</p>

    <nitro:slot name="footer">
        <button nitro:click="publish($post['id'])">Publish</button>
    </nitro:slot>
</nitro:card>
<div class="card">
    {{ $slot }}

    @if ($slots->has('footer'))
        <footer>{{ $slots['footer'] }}</footer>
    @endif
</div>

To check whether a slot was given, use $slots->has('footer') or $slot->isEmpty(). Slot content belongs to the parent, so its handlers and nitro:model act on the parent component. In the example above, publish() is the parent's method.

Rendering content elsewhere with teleport

A modal or a dropdown often needs to render outside the component, at the end of the page. @teleport renders its content where the layout has the matching @portal, while the content stays part of the component:

Blade
{{-- In the component: --}}
@teleport('modals')
    <div class="modal">...</div>
@endteleport

{{-- In the layout, where the content appears: --}}
@portal('modals')

Reading config in the browser

In a view, config() can only read the keys listed in nitro.public_config, which is app.name by default. Their values are sent to the browser, so only list keys that can be public:

PHP
// config/nitro.php
'public_config' => ['app.name', 'shop.currency'],

Next steps