Docs / Inlay / Blocks

Blocks

A block is a small piece - a heading, some text, a button, an image - that an editor places inside a section, in any order. A section opts in by declaring a blocks() field.

Adding a blocks area to a section

Settings::make()
    ->text('heading')->label('Heading')->default('Why it matters')
    ->blocks('content')->label('Content')->allow(['text', 'button'])->max(6)->default([
        ['type' => 'text', 'settings' => ['body' => '<p>Start here.</p>']],
    ])
  • ->allow([...]) limits which block handles the area accepts. Without it, every registered block is allowed.
  • ->max(n) caps how many blocks the area holds.
  • ->default([...]) seeds the area: a list of ['type' => handle, 'settings' => [...]].

allow() and max() only apply to a blocks() field. A blocks() field must be at the top level of a section - not inside a repeater or group - and blocks can't contain blocks.

Rendering blocks

The field arrives in your view as a WeArePixel\Inlay\Support\BlockList. Echo it to render every block in order:

<section class="py-24">
    <h2>{{ $heading }}</h2>
    {{ $content }}
</section>

Loop to wrap each one. Each item is a RenderedBlock that echoes its HTML and exposes $block->type and $block->settings:

@foreach ($content as $block)
    <div class="card card--{{ $block->type }}">{{ $block }}</div>
@endforeach

BlockList also has isEmpty() and count().

A block whose type has since been unregistered is skipped when rendering, and a block that throws while rendering is skipped and logged.

Writing a block

Single-file

The same shape as a single-file section, with block(Block::make(...)). The landing starter kit's button block:

{{-- resources/views/inlay/blocks/button.blade.php --}}
<?php

use WeArePixel\Inlay\Schema\Settings;
use WeArePixel\Inlay\SingleFile\Block;
use WeArePixel\Inlay\Support\LinkValue;

block(
    Block::make('button')
        ->name('Button')
        ->description('A link styled as a button.')
        ->settings(
            Settings::make()
                ->link('link')->label('Link')->default(new LinkValue('Find out more', '#'))
                ->select('style')->label('Style')->options(['primary' => 'Primary', 'secondary' => 'Secondary'])->default('primary')
        )
);
?>
@if ($link->url !== '')
    <a href="{{ $link->url }}" class="{{ $style === 'secondary' ? 'btn btn-outline' : 'btn' }}">{{ $link->label }}</a>
@endif

Register the directory:

Inlay::singleFileBlocks(resource_path('views/inlay/blocks'));
Inlay::singleFileBlock(resource_path('views/inlay/blocks/button.blade.php')); // or one file

Class-based

Implement WeArePixel\Inlay\Contracts\InlayBlock, describe it with #[BlockMeta] (same fields as #[SectionMeta]), and register it in config('inlay.blocks'):

'blocks' => [
    'quote' => \App\Inlay\Blocks\Quote::class,
],

Settings work exactly as for sections - see Settings.

Storage and publishing

Blocks are stored inside their section's settings, so drafts, publishing, version history and restore cover them with no extra work.

In the editor

A section with a blocks area shows its blocks in the inspector. Editors add a block from the allowed list, select one to edit its settings, and move it up or down, duplicate or remove it. The add and duplicate controls are disabled once the area reaches its max(). Clicking a block in the preview selects it - see The editor.

Discovery Call

One call.
We'll both know.

20 minutes to walk through your project, ask the hard questions, and work out honestly if we're the right team for it.

  • One call per day - it gets our full attention
  • Australia's only Laravel Premier Partner
  • Trusted by Chemist Warehouse, HelloFresh and Youfoodz
  • Senior engineers only - no juniors on your project
  • Brisbane-based, onshore team

Press Esc to close  ·  B to reopen