I’m still new to Sage 9 and perhaps this post should be made on a Laravel support page, but the Roots community is really great so I’m starting here.
I have a case where I need to display taxonomies terms as an archive list, basically an archive of taxonomy terms, with images, headlines, and description text for each term. I’d like to use the same archive.blade.php template if possible rather than duplicating (and maintaining) two different templates that output content with nearly the same visual display.
But post archives and taxonomy terms get their data through different processes — images from
the_post_thumbnail vs an ACF’s
get_field('image', $term);, titles from
Currently I am generating the taxonomy term ‘archive’ with a WordPress page and a page template that loops through the terms. The taxonomy is called
Series and the template is
What would be an effective strategy for building an archive template that is shared between regular post archives and a taxonomy term archive?
Thanks in advance.
Off the top of my head, the best option that gets you close to where you want to be would be something like this:
- Have two templates, one for your normal archive and one for your custom taxonomy term list page.
- Abstract the template code used for each post/term into a partial file. Create additional partials for other shared template components as appropriate.
- In your partial files, your code should avoid referring to data that is accessed differently for terms than for posts. Instead, use variable names that abstract from the underlying sources. For example, refer to
$thumbnail instead of
- Create a controller for each template (or edit the existing one). For each abstraction variable you used in your partials, create a method in your controller that returns the needed data. For example, a method named
thumbnail() would populate the
$thumbnail variable in your template.
@include your partials in your two templates as needed.
Use two templates but move common template code into partials that can be reused in each template. Use a controller for each template to populate the needed data into variables that are accessed in the shared partials.
Let me know if you have any questions!
Thanks for this suggestion! It’s similar to how I would have done it in Timber & Twig, and I don’t know why I didn’t think of it. It makes total sense.
I guess it’s because, unlike Timber where the flow is always from template/controller to Twig/view (and from there to child Twigs), Blade templates seem to also be able to modify a parent template via things like @extend and @yield, neither of which I can wrap my head around yet.
I was getting attached to Timber’s strict controller/view separation and getting frustrated with Blade returning me to a mixture of functions and views in one template, but now that I think about it there’s nothing stopping me from using Blade in a stricter controller/view model.