Blade and ACF flexible content best practice

I’ve put standalone variables for ACF in a Controller but can’t figure out the best practice for more complex variables with ACF such as the flexible content fields. For example, if I have a simple Flexible content type of “FlexibleContent” that includes 2 conditional rows, “Row1, Row2” with a subfield of “text”, here is how I would script it using PHP.

<?php
if( have_rows('FlexibleContent') ):
    while ( have_rows('FlexibleContent') ) : the_row();

        if( get_row_layout() == 'Row1' ):
          the_sub_field('TextBox');

        elseif( get_row_layout() == 'Row2' ):
           the_sub_field('TextBox2');

        endif;
    endwhile;
?>

However, using Blade with the Controllers, I’m not sure the best practice to put the logic. Do I just include the PHP? If I try to translate it to Blade with something like the following, I get a parse error. Still struggling to understand how the controller fits into the Blade view and how to pass complex data in. Also, what would be the best practice to declare the get_row_layout() == 'Row1' from the controller? If you have any links/resources, I’d love to read them and thanks in advanced!

Controller

public function flexiblecontent()
{
    return (object) array(
        'layout' => have_rows('FlexibleContent'),
        'subfield' => the_sub_field('TextBox'),
        'subfield2' => the_sub_field('TextBox2'),
    );
}

Blade View

@if( {{$flexiblecontent->layout}} ):
    @while( {{$flexiblecontent->layout}} ) @php( the_row() )

    @if ( get_row_layout() == 'Row1' ) @php
      {{$flexiblecontent->subfield}}

    @elseif( get_row_layout() == 'download' ):
      {{$flexiblecontent->subfield2}}
    
    @endif
  endwhile;
@endif

Related:

1 Like