Blade Template not finding Composer class

I have this foreach loop in a template

<!-- resources/views/partials/content-single-in-depth.blade.php -->
@if( $flexible_content['content'] )
  @foreach( $flexible_content['content'] as $content )
    @switch( $content['content_type'] )
      @case( 'title-block' )
        @include('partials.flex-title-block', $content)
        @break

      @case( 'full-width-image' )
        @include('partials.flex-full-width-image', $content)
        @break
    @endswitch
  @endforeach
@endif

The Full Width template looks like this:

<!--  resources/views/partials/flex-full-width-image.blade.php -->
<div class="{{ $component_class }}">
  <div class="row no-gutters">
    <div class="col-sm-12">
        <figure>
          {{$image}}
        </figure>
    </div>
  </div>
</div>

Finally, the Composer class:

// app/View/Composers/FlexFullWidthImage.php
<?php

namespace App\View\Composers;

use Roots\Acorn\View\Composer;

class FlexFullWidthImage extends Composer
{
    public function with($data, $view)
    {
        return [
            'image'           => image($data),
            'component_class' => classes($data),
        ];
    }

    function image($data)
    {
        return wp_get_attachment_image( $data['image'], $data['fw_image_size'], false, $data['image_classes'] );
    }

    function classes($data)
    {
        $classes = array_merge( ['fluid-container', 'westex-vr-full-width'], $data['narrow_class'] );
        return implode( ', ', $classes );
    }
}

When calling the content-single-in-depth.blade.php and getting to that include I get an error Undefined variable: component_class. Which I’m assuming means that the Composer file isn’t being called. IS this how I should be doing this in a loop? I have increasingly more complicated ACF flexible layouts to render, so if not what would be the best way to pass an array of values to template? I know of Components but unless I’ve misunderstood them everything needs to be a string that gets passed to them.

It appears you are missing your views property that tells the class which views to be run for

    /**
     * List of views served by this composer.
     *
     * @var array
     */
    protected static $views = [
        'partials.content-single-in-depth',
    ];

If you use wp acorn commands to generate these composer it should come with one of those automatically.

The answer above is correct but I just want to chime in that you can pass PHP expressions and variables to components

You may also want to check out dynamic components if your Blade version supports it. It could potentially allow you to get rid of the switch/case in your template.

Note that you can also use Acorn’s view autoloading to skip the views property if you name your composers correctly: Acorn 2.x: Blade Templates | Roots Documentation

1 Like

Weird but my Local dev was on an older version of PHP(7.3) updating that seemed to fix it? Or possibly the server reboot.

Ah, right, so popping that composer into a Partials folder could work also, right?

I believe you’d need to update the namespace to be App\View\Composers\Partials as well, but yes, it should.

2 Likes