Roots Discourse

Controller for ACF flexible content repeater for headline with different colors

Hi everyone,

I’m currently working on a client project that requires headlines with differently colored segments. This type of headline is required for several layouts in a flexible content field.

The logic in my view is pretty straightforward:

  $row_count = get_sub_field('headline');
  if (!empty($row_count)) {
  $row_count = count($row_count);


  <h2 class="text-uppercase">

    @while(have_rows('headline')) @php the_row() @endphp

				@php $color = get_sub_field('color') @endphp

				<span class="text-{{ $color }}">@php the_sub_field('headline_segment') @endphp</span>
				@if (get_row_index() < $row_count)




However, I’d like to take advantage of the way Sage is intended to be used and rather have this kind of code placed in my controller.

Unfortunately, I’m really new to working with controllers and am not quire sure how I would approach this since I assume I would have create a controller for each specific flexible content partial.

I’d really appreciate any hints. :slight_smile:

Using a controller is great but I don’t think you have enough logic yet to need to. I’m on my phone and this code is not tested but I would write it like:

@foreach(get_sub_field('headline') as $headline)
<span class="text-{{ $headline[‘color’] }}">{{ $headline[‘headline_segment’] }}</span>


1 Like

Thanks a lot for your reply and your code example. That is so much more efficient than my approach!
I really need to dig into Blade and get to know all these useful little functions (like the loop variable).

And yeah, using a controller probably only makes sense for more complicated things. :slight_smile:

This topic was automatically closed after 42 days. New replies are no longer allowed.