Outputting

Once you've stored some flexible content in the database you are probably going to want to display it on the frontend.

As long as you've added the Yadda\Enso\Crud\Traits\HasFlexibleFields trait to your model you can use the @flexibleField blade directive to output your data. E.g.:

@flexibleField($my_model, 'my_field', 'some-class', 'some-template', 'some-id')

In this example:

  • $my_model - an instance of a model with the HasFlexibleFields trait applied
  • my-field - the name of the flexible content column
  • some-class (optional) - a class which will be used as the base for a BEM class structure instead of the default class flexible-content
  • some-tempalte (optional) - a template which will be used instead of the default template enso-crud::flex-partials.flexible-content. Passing null will use the default.
  • some-id (optional) - an id which is used to generate row id's instead of using the field_name.

Views

When the @flexibleField directive is used it will run through each row in the data.

The default row view will output each block (i.e. field) within the row and output a corresponding view. There are default block-level templates for each field type. You can override these views with your own, either at the row or block level.

Row

Custom views for your rows should be created in views/vendor/enso-crud/flex-partials/rows/. Each file should be named after the output of $row->getType(), e.g. hero.blade.php. If you're not sure what this is, edit (or create) views/vendor/enso-crud/flex-partials/default-row.blade.php to include the following

@php
  dump($row->getType())
@endphp

The $row variable used above should always be available in a row view and is an instance of Yadda\Enso\Crud\Handlers\FlexibleRow.

This class includes the helper methods block($name) and setting($name) which allow you to get a block or settings block by its name.

Block

Blocks in this context represent the sub-fields with a Row. You can think of them as fields but having a separate word prevents confusion. Or at least reduces ambiguity. It is still confusing!

You may override a block view when it is used in specific row type. For example a text block within an intro row might use this view: E.g. views/vendor/enso-crud/flex-partials/blocks/intro/text.blade.php

In general, the structure of these URLs is views/vendor/enso-crud/flex-partials/blocks/{$row->getType()}/{$block->getType()}.blade.php.

If the above does not exist, a default view will be loaded from views/vendor/enso-crud/blocks/. E.g. views/vendor/enso-crud/text.blade.php

In Block views you will have access to a variable called block which is an instance of Yadda/Enso/Crud/Handlers/FlexibleBlock.