News

Template Extraction

Published

Christopher Pitt has a new post talking about template extraction and keeping your html DRY.

This got me thinking about how we focus so much on making a score of abstraction layers, in the hopes that our code will be “less messy” or “easier to test” or even just because “Jeffrey said so”, but we often ignore what the templates look like.

I extract my templates in a similar fashion but I always use includes instead of sections and yields. For me it just seems simpler.

Here is an example of a few of my templates:

@include('inc.users', ['users' => $users, 'type' => 'sidebar'])

and then inc.users:

<div class="{{ $type }}">
@foreach ($users as $user)
@include('inc.user_item')
@endforeach
</div>

I’m sure both of these have positives and negatives but I’ve always like the @include mainly because I can optionally pass an array.

Have your say… How are you structuring your views? Any tips that I’m missing?

Eric L. Barnes photo

Eric is the creator of Laravel News and has been covering Laravel since 2012.

Filed in

Sponsored

acquaintsoft logo
Acquaint Softtech

Hire Laravel developers with AI expertise at $20/hr. Get started in 48 hours.

Visit Acquaint Softtech

The latest

View all →
PhpStorm 2026.2 Released image

PhpStorm 2026.2 Released

Read article
Laravel Doctor: Diagnose Your App With One Artisan Command image

Laravel Doctor: Diagnose Your App With One Artisan Command

Read article
CPX: The Composer Package Executor for PHP image

CPX: The Composer Package Executor for PHP

Read article
Laravel AI SDK Adds Human-in-the-Loop Tool Approval image

Laravel AI SDK Adds Human-in-the-Loop Tool Approval

Read article
Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals image

Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals

Read article
Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs image

Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs

Read article