# $field->toBlocks()

Converts a yaml or json field to a Blocks object

****

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

****

```php
$field->toBlocks(): Kirby\Cms\Blocks
```

## Return type

`Kirby\Cms\Blocks`

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


<h2 id="blocks-in-your-templates"><a href="#blocks-in-your-templates" tabindex="-1">Blocks in your templates</a></h2>
<p>If you don't want to care about the HTML for each individual block, you can echo the entire blocks collection to render all blocks.</p>
<figure class="code">
<pre><code class="language-php">&lt;?= $page-&gt;myBlocksField()-&gt;toBlocks() ?&gt;</code></pre>
</figure>
<h3 id="blocks-in-your-templates__looping-through-blocks"><a href="#blocks-in-your-templates__looping-through-blocks" tabindex="-1">Looping through blocks</a></h3>
<p>Looping through blocks to control their HTML can be very powerful. You can assign custom CSS classes, IDs for links and more.</p>
<p>You don't need to render the HTML for each individual block in the loop though. You can wrap the block with your custom HTML and then echo the <code class="type">$block</code> object to render the matching block snippet.</p>
<figure class="code">
<pre><code class="language-php">&lt;?php foreach ($page-&gt;myBlocksField()-&gt;toBlocks() as $block): ?&gt;
&lt;div id="&lt;?= $block-&gt;id() ?&gt;" class="block block-type-&lt;?= $block-&gt;type() ?&gt;"&gt;
  &lt;?= $block ?&gt;
&lt;/div&gt;
&lt;?php endforeach ?&gt;</code></pre>
</figure>
<h3 id="blocks-in-your-templates__manually-loading-snippets"><a href="#blocks-in-your-templates__manually-loading-snippets" tabindex="-1">Manually loading snippets</a></h3>
<p>Sometimes you might wish to customize the way block snippets are loaded. Maybe you want to inject more snippet variables.</p>
<figure class="code">
<pre><code class="language-php">&lt;?php foreach ($page-&gt;myBlocksField()-&gt;toBlocks() as $block): ?&gt;
&lt;div id="&lt;?= $block-&gt;id() ?&gt;" class="block block-type-&lt;?= $block-&gt;type() ?&gt;"&gt;
  &lt;?php snippet('blocks/' . $block-&gt;type(), [
    'block' =&gt; $block,
    'theme' =&gt; 'dark'
  ]) ?&gt;
&lt;/div&gt;
&lt;?php endforeach ?&gt;</code></pre>
</figure>
<p>… or load snippets from a different location …</p>
<figure class="code">
<pre><code class="language-php">&lt;?php foreach ($page-&gt;myBlocksField()-&gt;toBlocks() as $block): ?&gt;
&lt;div id="&lt;?= $block-&gt;id() ?&gt;" class="block block-type-&lt;?= $block-&gt;type() ?&gt;"&gt;
  &lt;?php snippet('blocks/custom/' . $block-&gt;type(), [
    'block' =&gt; $block,
    'theme' =&gt; 'dark'
  ]) ?&gt;
&lt;/div&gt;
&lt;?php endforeach ?&gt;</code></pre>
</figure>
<details class="since list-none" open="1">
<summary >
	Since <a href="https://github.com/getkirby/kirby/releases/tag/5.5.0">5.5.0</a></summary>
<div>
	<h3 id="blocks-in-your-templates__debugging"><a href="#blocks-in-your-templates__debugging" tabindex="-1">Debugging</a></h3>
<p>Bugs in your blog snippets will throw an exception in debug mode, but will otherwise be swallowed.</p></div>
</details>