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

masteringlaravel logo
Laravel Code Review

Get expert guidance in a few days with a Laravel code review

Visit Laravel Code Review

The latest

View all →
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
Blade Formatting in Laravel Pint image

Blade Formatting in Laravel Pint

Read article
Inertia DevTools Is Now on the Chrome Web Store image

Inertia DevTools Is Now on the Chrome Web Store

Read article