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 →
Agent Run Observability in Laravel AI SDK 0.11 image

Agent Run Observability in Laravel AI SDK 0.11

Read article
Debounced Queued Event Listeners in Laravel image

Debounced Queued Event Listeners in Laravel

Read article
Statamic Mailables Viewer Previews Laravel Emails in the Control Panel image

Statamic Mailables Viewer Previews Laravel Emails in the Control Panel

Read article
Laravel Tackle: Run an AI Coding Agent in Your Laravel App image

Laravel Tackle: Run an AI Coding Agent in Your Laravel App

Read article
Queue::forward(): Reroute Laravel Queues in One Place image

Queue::forward(): Reroute Laravel Queues in One Place

Read article
Laravel Read-Through Filesystem: Lazy Storage Migration image

Laravel Read-Through Filesystem: Lazy Storage Migration

Read article