# $blocks

A collection of blocks

****

- Full class name: `Kirby\Cms\Blocks`
- Source: <https://github.com/getkirby/kirby/tree/5.5.3/src/Cms/Blocks.php#L26>

****

### $blocks

- [$blocks->__call()](https://getkirby.com/docs/reference/objects/cms/blocks/__call.md)
- [new Blocks()](https://getkirby.com/docs/reference/objects/cms/blocks/__construct.md)
- [$blocks->__debugInfo()](https://getkirby.com/docs/reference/objects/cms/blocks/__debug-info.md)
- [$blocks->__get()](https://getkirby.com/docs/reference/objects/cms/blocks/__get.md)
- [$blocks->__isset()](https://getkirby.com/docs/reference/objects/cms/blocks/__isset.md)
- [$blocks->__set()](https://getkirby.com/docs/reference/objects/cms/blocks/__set.md)
- [$blocks->__toString()](https://getkirby.com/docs/reference/objects/cms/blocks/__to-string.md)
- [$blocks->__unset()](https://getkirby.com/docs/reference/objects/cms/blocks/__unset.md)
- [$blocks->add()](https://getkirby.com/docs/reference/objects/cms/blocks/add.md)
- [$blocks->append()](https://getkirby.com/docs/reference/objects/cms/blocks/append.md)
- [$blocks->chunk()](https://getkirby.com/docs/reference/objects/cms/blocks/chunk.md)
- [$blocks->clone()](https://getkirby.com/docs/reference/objects/cms/blocks/clone.md)
- [$blocks->count()](https://getkirby.com/docs/reference/objects/cms/blocks/count.md)
- [$blocks->current()](https://getkirby.com/docs/reference/objects/cms/blocks/current.md)
- [$blocks->data()](https://getkirby.com/docs/reference/objects/cms/blocks/data.md)
- [$blocks->empty()](https://getkirby.com/docs/reference/objects/cms/blocks/empty.md)
- [$blocks->excerpt()](https://getkirby.com/docs/reference/objects/cms/blocks/excerpt.md)
- [$blocks->extend()](https://getkirby.com/docs/reference/objects/cms/blocks/extend.md)
- [Blocks::factory()](https://getkirby.com/docs/reference/objects/cms/blocks/factory.md)
- [$blocks->field()](https://getkirby.com/docs/reference/objects/cms/blocks/field.md)
- [$blocks->filter()](https://getkirby.com/docs/reference/objects/cms/blocks/filter.md)
- [$blocks->filterBy()](https://getkirby.com/docs/reference/objects/cms/blocks/filter-by.md)
- [$blocks->find()](https://getkirby.com/docs/reference/objects/cms/blocks/find.md)
- [$blocks->findBy()](https://getkirby.com/docs/reference/objects/cms/blocks/find-by.md)
- [$blocks->findByKey()](https://getkirby.com/docs/reference/objects/cms/blocks/find-by-key.md)
- [$blocks->first()](https://getkirby.com/docs/reference/objects/cms/blocks/first.md)
- [$blocks->flip()](https://getkirby.com/docs/reference/objects/cms/blocks/flip.md)
- [$blocks->get()](https://getkirby.com/docs/reference/objects/cms/blocks/get.md)
- [$blocks->getAttribute()](https://getkirby.com/docs/reference/objects/cms/blocks/get-attribute.md)
- [$blocks->getIterator()](https://getkirby.com/docs/reference/objects/cms/blocks/get-iterator.md)
- [$blocks->getMethod()](https://getkirby.com/docs/reference/objects/cms/blocks/get-method.md)
- [$blocks->group()](https://getkirby.com/docs/reference/objects/cms/blocks/group.md)
- [$blocks->groupBy()](https://getkirby.com/docs/reference/objects/cms/blocks/group-by.md)
- [$blocks->has()](https://getkirby.com/docs/reference/objects/cms/blocks/has.md)
- [$blocks->hasMethod()](https://getkirby.com/docs/reference/objects/cms/blocks/has-method.md)
- [$blocks->hasType()](https://getkirby.com/docs/reference/objects/cms/blocks/has-type.md)
- [$blocks->indexOf()](https://getkirby.com/docs/reference/objects/cms/blocks/index-of.md)
- [$blocks->intersection()](https://getkirby.com/docs/reference/objects/cms/blocks/intersection.md)
- [$blocks->intersects()](https://getkirby.com/docs/reference/objects/cms/blocks/intersects.md)
- [$blocks->isEmpty()](https://getkirby.com/docs/reference/objects/cms/blocks/is-empty.md)
- [$blocks->isEven()](https://getkirby.com/docs/reference/objects/cms/blocks/is-even.md)
- [$blocks->isNotEmpty()](https://getkirby.com/docs/reference/objects/cms/blocks/is-not-empty.md)
- [$blocks->isOdd()](https://getkirby.com/docs/reference/objects/cms/blocks/is-odd.md)
- [$blocks->join()](https://getkirby.com/docs/reference/objects/cms/blocks/join.md)
- [$blocks->key()](https://getkirby.com/docs/reference/objects/cms/blocks/key.md)
- [$blocks->keyOf()](https://getkirby.com/docs/reference/objects/cms/blocks/key-of.md)
- [$blocks->keys()](https://getkirby.com/docs/reference/objects/cms/blocks/keys.md)
- [$blocks->last()](https://getkirby.com/docs/reference/objects/cms/blocks/last.md)
- [$blocks->limit()](https://getkirby.com/docs/reference/objects/cms/blocks/limit.md)
- [$blocks->map()](https://getkirby.com/docs/reference/objects/cms/blocks/map.md)
- [$blocks->next()](https://getkirby.com/docs/reference/objects/cms/blocks/next.md)
- [$blocks->not()](https://getkirby.com/docs/reference/objects/cms/blocks/not.md)
- [$blocks->nth()](https://getkirby.com/docs/reference/objects/cms/blocks/nth.md)
- [$blocks->offset()](https://getkirby.com/docs/reference/objects/cms/blocks/offset.md)
- [$blocks->paginate()](https://getkirby.com/docs/reference/objects/cms/blocks/paginate.md)
- [$blocks->pagination()](https://getkirby.com/docs/reference/objects/cms/blocks/pagination.md)
- [$blocks->parent()](https://getkirby.com/docs/reference/objects/cms/blocks/parent.md)
- [Blocks::parse()](https://getkirby.com/docs/reference/objects/cms/blocks/parse.md)
- [$blocks->pluck()](https://getkirby.com/docs/reference/objects/cms/blocks/pluck.md)
- [$blocks->prepend()](https://getkirby.com/docs/reference/objects/cms/blocks/prepend.md)
- [$blocks->prev()](https://getkirby.com/docs/reference/objects/cms/blocks/prev.md)
- [$blocks->query()](https://getkirby.com/docs/reference/objects/cms/blocks/query.md)
- [$blocks->random()](https://getkirby.com/docs/reference/objects/cms/blocks/random.md)
- [$blocks->remove()](https://getkirby.com/docs/reference/objects/cms/blocks/remove.md)
- [$blocks->rewind()](https://getkirby.com/docs/reference/objects/cms/blocks/rewind.md)
- [$blocks->search()](https://getkirby.com/docs/reference/objects/cms/blocks/search.md)
- [$blocks->set()](https://getkirby.com/docs/reference/objects/cms/blocks/set.md)
- [$blocks->shuffle()](https://getkirby.com/docs/reference/objects/cms/blocks/shuffle.md)
- [$blocks->slice()](https://getkirby.com/docs/reference/objects/cms/blocks/slice.md)
- [$blocks->sort()](https://getkirby.com/docs/reference/objects/cms/blocks/sort.md)
- [Blocks::sortArgs()](https://getkirby.com/docs/reference/objects/cms/blocks/sort-args.md)
- [$blocks->sortBy()](https://getkirby.com/docs/reference/objects/cms/blocks/sort-by.md)
- [$blocks->toArray()](https://getkirby.com/docs/reference/objects/cms/blocks/to-array.md)
- [$blocks->toHtml()](https://getkirby.com/docs/reference/objects/cms/blocks/to-html.md)
- [$blocks->toJson()](https://getkirby.com/docs/reference/objects/cms/blocks/to-json.md)
- [$blocks->toString()](https://getkirby.com/docs/reference/objects/cms/blocks/to-string.md)
- [$blocks->update()](https://getkirby.com/docs/reference/objects/cms/blocks/update.md)
- [$blocks->valid()](https://getkirby.com/docs/reference/objects/cms/blocks/valid.md)
- [$blocks->values()](https://getkirby.com/docs/reference/objects/cms/blocks/values.md)
- [$blocks->when()](https://getkirby.com/docs/reference/objects/cms/blocks/when.md)
- [$blocks->without()](https://getkirby.com/docs/reference/objects/cms/blocks/without.md)

## What are blocks?

When you want to work with the content of a <a href="https://getkirby.com/docs/reference/panel/fields/blocks">Blocks field</a>, you would use the <a class="type-link" href="https://getkirby.com/docs/reference/templates/field-methods/to-blocks"><code class="type type-method">toBlocks</code></a> field method to convert it to a Blocks collection - a collection of `Kirby\Cms\Block` objects. The Blocks collection makes it easy to render the entire set of blocks or loop through blocks and create individual HTML for them.

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