Template Extraction
Published on by Eric L. Barnes
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 is the creator of Laravel News and has been covering Laravel since 2012.