Re-using a markup pattern with different data

I have a markup pattern that will be used through the site, taking data from an ACF clone field with prefixed names. Here’s an example with a set of ACF data that is included in a CPT archive that extends the main App lasyout:

@extends('layouts.app')

@section('content')

  <div class="title-group">
    @if ($producer_intro_subtitle)
      <h4>{!! $producer_intro_subtitle !!}</h4>
    @endif
    @if ($producer_intro_subtitle)
      <h1>{!! $producer_intro_title !!}</h1>
    @endif
    @if ($producer_intro_text)
      <p>{!! $producer_intro_text !!}</p>
    @endif
    @if ($producer_intro_text)
      <a href="{!! $producer_fine_wine_list_download['url']!!}">{!! $producer_fine_wine_list_download['name']!!}</a>
    @endif
  </div>

  @php ArchiveProducer::producerRegions() @endphp

  @if (!have_posts())
    <div class="alert alert-warning">
      {{ __('Sorry, no results were found.', 'sage') }}
    </div>
    {!! get_search_form(false) !!}
  @endif

  @while (have_posts()) @php the_post() @endphp
    @include('partials.content-'.get_post_type())
  @endwhile

  {!! get_the_posts_navigation() !!}
@endsection

I’d like to use this pattern again on other templates, accpeting different data. I could repeat the same markup and swap out the variables, but that isn’t the best approach.

I am thinking I should create a new layout in views/layouts:, like this:

<section class="title-group">
  <h4>@yield('title-group__subtitle')</h4>
  <h1>@yield('title-group__title')</h1>
  <p>@yield('title-group__content')</p>
  <a href="@yield('title-group__link')">@yield('title-group__link-name')</a>
</section>

Then extend that into my CPT archive (or elsewhere) and replace the yields with the data. However this is where I’m coming unstuck.

I’m imagining something like this:

@extends('title-group',['title-group__title' => '{!! $producer_intro_title !!}'])

But cannot figure it out. Any suggestions? Or is this just the wrong way to think about it?

IMO @extends is the wrong tool for the job; this seems like a perfect opportunity to use @include. Just create a Blade that contains the bit you want to re-use, and give it more generic field names, i.e.:

// section-title-group.blade.php

<div class="title-group">
  @isset($subtitle)
    <h4>{!! $subtitle !!}</h4>
  @endisset
  @isset($title)
    <h1>{!! $title !!}</h1>
  @endisset
  @isset($intro)
    <p>{!! $intro !!}</p>
  @endisset
  @isset($link)
    <a href="{!! $link['url']!!}">{!! $link['name']!!}</a>
  @endisset
</div>

Then you can call it in your templates, and pass the necessary data to it:

@extends('layouts.app')

@section('content')
  @include('section-title-group', [
    'subtitle' => $producer_intro_subtitle,
    'title' => $producer_intro_title,
    'intro' => $producer_intro_text,
    'text' => $producer_intro_text,
    'link' => $producer_fine_wine_list_download,
  ])

  @php ArchiveProducer::producerRegions() @endphp
  
  ...

Makes perfect sense. Works well. Thank you verty much.

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