# Layout

A visual editor for complex multi-column layouts

****

- Read more: <https://getkirby.com/docs/guide/blueprints/fields>
- Source: <https://github.com/getkirby/kirby/tree/5.5.3/src/Form/Field/LayoutField.php>

****

<figure class="image"><a data-lightbox href="https://assets.getkirby.com/media/pages/docs/reference/panel/fields/layout/5e7194c758-1717846665/layout.png?width=2136&amp;height=2986&amp;enlarge=0"><img alt="" class="rounded" height="1292" loading="lazy" sizes="auto" src="https://assets.getkirby.com/media/pages/docs/reference/panel/fields/layout/5e7194c758-1717846665/layout.png?width=924&amp;height=1292&amp;enlarge=0" srcset="https://assets.getkirby.com/media/pages/docs/reference/panel/fields/layout/5e7194c758-1717846665/layout.png?width=462&amp;height=646&amp;enlarge=0 462w, https://assets.getkirby.com/media/pages/docs/reference/panel/fields/layout/5e7194c758-1717846665/layout.png?width=690&amp;height=965&amp;enlarge=0 690w, https://assets.getkirby.com/media/pages/docs/reference/panel/fields/layout/5e7194c758-1717846665/layout.png?width=924&amp;height=1292&amp;enlarge=0 924w, https://assets.getkirby.com/media/pages/docs/reference/panel/fields/layout/5e7194c758-1717846665/layout.png?width=1380&amp;height=1929&amp;enlarge=0 1380w, https://assets.getkirby.com/media/pages/docs/reference/panel/fields/layout/5e7194c758-1717846665/layout.png?width=1848&amp;height=2583&amp;enlarge=0 1848w" width="924"></a></figure>

## Getting started

By default the layout field comes with a single 1-column layout and all the default fieldsets from the Blocks field.

```yaml
fields:
  layout:
    type: layout
```

## Defining your own layouts

Defining available layouts is simple: each layout is list of column widths. i.e. `1/2, 1/2` or `1/4, 3/4` …

If the sum of the layout is greater than one, the other columns will be wrapped to the next line.

```yaml
fields:
  layout:
    type: layout
    layouts:
      - "1/1"
      - "1/2, 1/2"
      - "1/4, 1/4, 1/4, 1/4"
      - "1/1, 1/3, 2/3"
      - "1/1, 2/3, 1/3"
      - "1/2, 1/2, 1/3, 1/3, 1/3"
```

Those layouts will then show up in the layout selector when an editor creates a new row:

<figure class="image"><a data-lightbox href="https://assets.getkirby.com/media/pages/docs/reference/panel/fields/layout/3fa11ab119-1717846665/selector.png?width=477&amp;height=266&amp;enlarge=0"><img alt="" class="rounded" height="266" loading="lazy" sizes="auto" src="https://assets.getkirby.com/media/pages/docs/reference/panel/fields/layout/3fa11ab119-1717846665/selector.png?width=477&amp;height=266&amp;enlarge=0" srcset="https://assets.getkirby.com/media/pages/docs/reference/panel/fields/layout/3fa11ab119-1717846665/selector.png?width=462&amp;height=258&amp;enlarge=0 462w, https://assets.getkirby.com/media/pages/docs/reference/panel/fields/layout/3fa11ab119-1717846665/selector.png?width=477&amp;height=266&amp;enlarge=0 690w, https://assets.getkirby.com/media/pages/docs/reference/panel/fields/layout/3fa11ab119-1717846665/selector.png?width=477&amp;height=266&amp;enlarge=0 924w, https://assets.getkirby.com/media/pages/docs/reference/panel/fields/layout/3fa11ab119-1717846665/selector.png?width=477&amp;height=266&amp;enlarge=0 1380w, https://assets.getkirby.com/media/pages/docs/reference/panel/fields/layout/3fa11ab119-1717846665/selector.png?width=477&amp;height=266&amp;enlarge=0 1848w" width="477"></a></figure>

### Available widths

The layout field supports up to 12 columns and all are listed below.

- `1/1` `1/2` `1/3` `1/4` `1/6` `1/12`
- `2/2` `2/3` `2/4` `2/6` `2/12`
- `3/3` `3/4` `3/6` `3/12`
- `4/4` `4/6` `4/12`
- `5/6` `5/12`
- `6/6` `6/12`
- `7/12`
- `8/12`
- `9/12`
- `10/12`
- `11/12`
- `12/12`

## Customizing the selector

You can customize the size and number of columns in the layout selector to account for the number of different layouts you have defined via the `layouts` property.

```yaml
fields:
  layout:
    type: layout
    layouts:
      # ...
    selector:
      size: huge
      columns: 6
```

Available sizes: `small`, `medium` (default), `large`, `huge`
Default columns: 3

## Fieldsets

The layout field also accepts the `fieldsets` option from the blocks field to control blocks in columns.

The fieldsets setup is exactly the same. You can follow the instructions from the <a href="https://getkirby.com/docs/reference/panel/fields/blocks#defining-fieldsets">Blocks field</a>.

```yaml
fields:
  layout:
    type: layout
    layouts:
      - "1/1"
      - "1/2, 1/2"
      - "1/4, 1/4, 1/4, 1/4"
      - "1/3, 2/3"
      - "2/3, 1/3"
      - "1/3, 1/3, 1/3"
    fieldsets:
      - heading
      - text
      - image
```

## Layout settings

It's often useful to set additional class names, IDs or even something like background colors and images for layout sections. This can be achieved with layout settings.

Layout settings are defined globally. Each layout will then get a "Settings" button in its dropdown and open a settings drawer with the configured fields.

```yaml
fields:
  layout:
    type: layout
    layouts:
      - "1/1"
      - "1/2, 1/2"
      - "1/4, 1/4, 1/4, 1/4"
      - "1/3, 2/3"
      - "2/3, 1/3"
      - "1/3, 1/3, 1/3"
    fieldsets:
      - heading
      - text
      - image
    settings:
      fields:
        class:
          type: text
          width: 1/2
        id:
          type: text
          width: 1/2
        image:
          label: Background image
          type: files
```

<figure class="image"><a data-lightbox href="https://assets.getkirby.com/media/pages/docs/reference/panel/fields/layout/223bf6d103-1717846665/layout-settings.png?width=1519&amp;height=979&amp;enlarge=0"><img alt="" class="rounded" height="596" loading="lazy" sizes="auto" src="https://assets.getkirby.com/media/pages/docs/reference/panel/fields/layout/223bf6d103-1717846665/layout-settings.png?width=924&amp;height=596&amp;enlarge=0" srcset="https://assets.getkirby.com/media/pages/docs/reference/panel/fields/layout/223bf6d103-1717846665/layout-settings.png?width=462&amp;height=298&amp;enlarge=0 462w, https://assets.getkirby.com/media/pages/docs/reference/panel/fields/layout/223bf6d103-1717846665/layout-settings.png?width=690&amp;height=445&amp;enlarge=0 690w, https://assets.getkirby.com/media/pages/docs/reference/panel/fields/layout/223bf6d103-1717846665/layout-settings.png?width=924&amp;height=596&amp;enlarge=0 924w, https://assets.getkirby.com/media/pages/docs/reference/panel/fields/layout/223bf6d103-1717846665/layout-settings.png?width=1380&amp;height=889&amp;enlarge=0 1380w, https://assets.getkirby.com/media/pages/docs/reference/panel/fields/layout/223bf6d103-1717846665/layout-settings.png?width=1519&amp;height=979&amp;enlarge=0 1848w" width="924"></a></figure>

### Tabs

If you have a lot of settings, you can organize them in tabs:

```yaml
fields:
  layout:
    type: layout
    layouts:
      # …
    fieldsets:
      # …
    settings:
      tabs:
        content:
          fields:
            link:
              type: url
            text:
              type: text
        styles:
          fields:
            class:
              type: text
            id:
              type: text
```
## Reusing and extending layout settings

If you always use the same options in layouts and want to easily update all of them from a single file without repeating the options, you can reuse or extend the layout settings. Let's see how:

**Sample settings fields**

```yaml "/site/blueprints/fields/settings.yml"
fields:
  class:
    type: text
    width: 1/2
  id:
    type: text
    width: 1/2
  image:
    label: Background image
    type: files
```

**Simple reuse**

```yaml
fields:
  layout:
    type: layout
    settings: fields/settings
```

**Extended reuse**

```yaml
fields:
  layout:
    type: layout
    settings:
      extends: fields/settings
      fields:
        color:
          label: Background color
          type: text
          default: FFF
```

<h2 id="how-to-render-layouts-in-templates"><a href="#how-to-render-layouts-in-templates" tabindex="-1">How to render layouts in templates?</a></h2>
<p>Using the <a class="type-link" href="https://getkirby.com/docs/reference/templates/field-methods/to-layouts"><code class="type type-method">toLayouts</code></a> field method, you can retrieve a Layouts collection - a collection of <a class="type-link" href="https://getkirby.com/docs/reference/objects/cms/layout"><code class="type type-class">Kirby\Cms\Layout</code></a> objects:</p>
<figure class="code">
<pre><code class="language-php">&lt;?php foreach ($page-&gt;layout()-&gt;toLayouts() as $layout): ?&gt;
&lt;section class="grid" id="&lt;?= $layout-&gt;id() ?&gt;"&gt;
  &lt;?php foreach ($layout-&gt;columns() as $column): ?&gt;
  &lt;div class="column" style="--span:&lt;?= $column-&gt;span() ?&gt;"&gt;
    &lt;div class="blocks"&gt;
      &lt;?= $column-&gt;blocks() ?&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;?php endforeach ?&gt;
&lt;/section&gt;
&lt;?php endforeach ?&gt;</code></pre>
</figure>
<h2 id="calculate-the-column-span-value"><a href="#calculate-the-column-span-value" tabindex="-1">Calculate the column span value</a></h2>
<p>Each column in a layout has a <a class="type-link" href="https://getkirby.com/docs/reference/objects/cms/layout-column/width"><code class="type type-method">$column-&gt;width()</code></a> method which will return the width defined in the blueprint. (i.e. <code class="type">1/2</code>) but for many grid systems you need to know how many columns the current column should span in the grid. This can be done with the <a class="type-link" href="https://getkirby.com/docs/reference/objects/cms/layout-column/span"><code class="type type-method">$column-&gt;span()</code></a> method. The method calculates with a 12-column grid by default. So for example, if your column width is <code class="type">1/2</code> the span method would return a value of 6. If you are working with a different kind of grid system you can pass the number of columns like this: <code class="type type-none">$column-&gt;span(6)</code>:</p>
<figure class="code">
<pre><code class="language-php">&lt;?php foreach ($page-&gt;layout()-&gt;toLayouts() as $layout): ?&gt;
&lt;section class="6-column-grid" id="&lt;?= $layout-&gt;id() ?&gt;"&gt;
  &lt;?php foreach ($layout-&gt;columns() as $column): ?&gt;
  &lt;div class="column" style="--span:&lt;?= $column-&gt;span(6) ?&gt;"&gt;
    &lt;div class="blocks"&gt;
      &lt;?= $column-&gt;blocks() ?&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;?php endforeach ?&gt;
&lt;/section&gt;
&lt;?php endforeach ?&gt;</code></pre>
</figure>
<h2 id="working-with-individual-blocks"><a href="#working-with-individual-blocks" tabindex="-1">Working with individual blocks</a></h2>
<p>In some cases, you might even want to controll the way blocks within layouts are rendered. <a class="type-link" href="https://getkirby.com/docs/reference/objects/cms/layout-column/blocks"><code class="type type-method">$column-&gt;blocks()</code></a> will return a blocks collection that you can work with and create another nested foreach loop.</p>
<figure class="code">
<pre><code class="language-php">&lt;?php foreach ($page-&gt;layout()-&gt;toLayouts() as $layout): ?&gt;
&lt;section class="6-column-grid" id="&lt;?= $layout-&gt;id() ?&gt;"&gt;
  &lt;?php foreach ($layout-&gt;columns() as $column): ?&gt;
  &lt;div class="column" style="--span:&lt;?= $column-&gt;span(6) ?&gt;"&gt;
    &lt;div class="blocks"&gt;
      &lt;?php foreach ($column-&gt;blocks() as $block): ?&gt;
      &lt;div class="block block-type-&lt;?= $block-&gt;type() ?&gt;"&gt;
        &lt;?= $block ?&gt;
      &lt;/div&gt;
      &lt;?php endforeach ?&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;?php endforeach ?&gt;
&lt;/section&gt;
&lt;?php endforeach ?&gt;</code></pre>
</figure>
<h2 id="passing-the-layout-object-to-the-block-snippet"><a href="#passing-the-layout-object-to-the-block-snippet" tabindex="-1">Passing the layout object to the block snippet</a></h2>
<p>If you need to access the layout object in a block snippet, you need to pass it to the snippet manually.</p>
<figure class="code">
<pre><code class="language-php">&lt;?php foreach ($page-&gt;layout()-&gt;toLayouts() as $layout): ?&gt;
&lt;section class="6-column-grid" id="&lt;?= $layout-&gt;id() ?&gt;"&gt;
  &lt;?php foreach ($layout-&gt;columns() as $column): ?&gt;
  &lt;div class="column" style="--span:&lt;?= $column-&gt;span(6) ?&gt;"&gt;
    &lt;div class="blocks"&gt;
      &lt;?php foreach ($column-&gt;blocks() as $block): ?&gt;
      &lt;div class="block block-type-&lt;?= $block-&gt;type() ?&gt;"&gt;
        &lt;?php snippet('blocks/' . $block-&gt;type(), ['block' =&gt; $block, 'layout' =&gt; $layout]) ?&gt;
      &lt;/div&gt;
      &lt;?php endforeach ?&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;?php endforeach ?&gt;
&lt;/section&gt;
&lt;?php endforeach ?&gt;</code></pre>
</figure>

## Accessing layout settings

Layout settings are accessible via the `$layout->attrs()` method. In the example, we add a class name using the `class` field in the layout settings:

```php
<?php foreach ($page->layout()->toLayouts() as $layout): ?>
<section class="grid <?= $layout->attrs()->class() ?>" id="<?= $layout->id() ?>">
  <!-- rest of code -->
</section>
<?php endforeach ?>
```

You have also access to a shortcut, which allows you to call the settings field directly:

```php
<?php foreach ($page->layout()->toLayouts() as $layout): ?>
<section class="grid <?= $layout->class() ?>" id="<?= $layout->id() ?>">
  <!-- rest of code -->
</section>
<?php endforeach ?>
```