Views

Introduction

Writing views in Devisto is no different from writing views in a regular Laravel application. Therefore, knowledge of Laravel Blade is recommended before writing views in Devisto. Take a look at the official Laravel Blade documentation for more information.

You can consider the Views section in Devisto as the views folder in a Laravel app. That means, if you create a file foo/bar/baz.blade.php in your views, you can include it everywhere with @include('foo.bar.baz').

Stacks

Blade allows you to push to named stacks which can be rendered somewhere else in another view or layout. Devisto provides 2 stacks out of the box: styles and scripts. You can use them as follows:

@push('styles')
    <style>
        ...
    </style>
@endpush

@push('scripts')
    <script>
        ...
    </script>
@endpush

In your master template, you should include the directives @styles and @scripts, which render all the necessary styles and scripts for the project, along with these stacks.

Execute PHP

If you need to execute some longer PHP code that you would otherwise write in a controller, just include it in your view between @php and @endphp. Every variable you define can be used below it in the view:

@php
    $products = table('products')
        ->where('on_homepage', 1)
        ->orderBy('title')
        ->get();
@endphp

@foreach ($products as $product)
    ...
@endforeach

Blade Components

Blade Components are a convenient way to encapsulate and reuse logic in your views.

For example, if you put the following code in a components/foo.blade.php file:

@props([
    'title' => 'Default Title',
])

<div {{ $attributes->merge(['class' => 'bg-red-500']) }}>
    <h2>{{ $title }}</h2>
    {{ $slot }}
</div>

...you can use the component everywhere in your views like this:

<x-foo title="Custom Title" class="text-blue-500">
    Test
</x-foo>
Previous topic
← Resources
Next topic
Livewire →