Skip to content

Own layouts

When the layouts of Anvil are not enough – e.g. a card with an image, an address block or a two-column comparison – write a layout of your own.

Terminal window
php spark make:anvil-layout Fieldset

generates App\Layouts\Fieldset and its view. A layout extends Anvil\Schema\Component and implements:

Method Purpose
fields(): array all fields inside, also of nested layouts – the form validates and saves them
render(FormContext $context): string the HTML; render the inner components with their render($context)
visibleFields(string $operation): array the fields shown in an operation; return [] when the layout is hidden

hiddenOn() and columnSpan() come from Component. Layouts usually take the full width (protected int $columnSpan = self::FULL;).

Rules:

  • Render inner components through their render(); never print values yourself without esc().
  • Leave out hidden components ($component->isVisible($context->operation)).
  • No inline styles or scripts (Content Security Policy).
  • A grid inside: view('Anvil\Views\schema\grid', ['columns' => …, 'components' => …, 'context' => $context]).

A complete example: Extending Anvil.