# $field->toLayouts()

Parse layouts and turn them into Layout objects

****

- Source: <https://github.com/getkirby/kirby/tree/5.5.3/config/methods.php#L197>

****

```php
$field->toLayouts(): Kirby\Cms\Layouts
```

## Return type

`Kirby\Cms\Layouts`

This method modifies the existing `$field` object it is applied to and returns it again.


<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>