Rendering Blocks

When it is time to build a frontend, you will want to render a designed set of blocks, with all blocks in their proper order. When working with a model instance that uses the HasBlocks trait in a view, you can call the renderBlocks helper on it. This will render the list of blocks that were created from the CMS. By default, this function will loop over all the blocks and their child blocks. In each case, the function will look for a Blade view to render for a given block.

Create views for your blocks in the resources/views/site/blocks directory. Their filenames should match the block key specified in your Twill configuration and module form.

For the products block example above, a corresponding view would be resources/views/site/blocks/products.blade.php.

You can call the renderBlocks helper within a Blade file. Such a call would look like this:

1{!! $item->renderBlocks() !!}

If you have more block fields, you can get a specific one using:

1{!! $item->renderNamedBlocks('field-name') !!}

You can also specify alternate blade views for blocks. This can be helpful if you use the same block in 2 different modules of the CMS, but you want to have design flexibility in how each is rendered.

To do that, specify the block view file in your call to the renderBlocks helper like this

1{!! $work->renderBlocks([
2 'block-type' => 'view.path',
3 'block-type-2' => 'another.view.path'
4]) !!}

Within these Blade views, you will have access to a $block variable with helper functions available to retrieve the block content:

1{{ $block->input('inputNameYouSpecifiedInTheBlockFormField') }}
2{{ $block->translatedinput('inputNameYouSpecifiedInATranslatedBlockFormField') }}

If the block has a media field, you can refer to the Media Library documentation below to learn about the HasMedias trait helpers. Here's an example of how a media field could be rendered:

1{{ $block->image('mediaFieldName', 'cropNameFromBlocksConfig') }}
2{{ $block->images('mediaFieldName', 'cropNameFromBlocksConfig') }}

Modifying block data

Sometimes it can be useful to abstract some PHP you would usually put at the top of the blade file. This will keep your blade files cleaner and allow for easier logic writing.

See Block classes documentation for more details about the block class.

1<?php
2 
3namespace App\Twill\Block;
4 
5use A17\Twill\Services\Blocks\Block;
6 
7class ImagesBlock extends Block
8{
9 public function getData(array $data, \A17\Twill\Models\Block $block): array
10 {
11 $data = parent::getData($data, $block);
12 
13 foreach ($block->imagesAsArrays('blog_image', 'desktop') as $imageData) {
14 $data['images'][] = [
15 'src' => $imageData['src'],
16 'alt' => $imageData['alt']
17 ];
18 }
19 
20 return $data;
21 }
22}