I need to output various @foreach
loops throughout my blade template, however they all target the same table in my database, just fetching different fields. As an example, I am using @yield in my main layout and @section in the views for title/descriptions tags etc etc:
@section('title')
@foreach($store_listings as $fetch)
Website stores - {{ $fetch->city }}, {{ $fetch->country }}
@endforeach
@stop
@section('description')
@foreach($store_listings as $fetch)
List of Stores in {{ $fetch->city }}, {{ $fetch->country }}.
@endforeach
@stop
Then in layout.main:
<title>@yield('title')</title>
and
<meta name="description" content="@yield('description')" />
This repeated @foreach loops are present in other sections of my views on other pages. I try to work by the DRY method. What are the suggestion here to save me entering multiple foreach loops when all I wish to do is call some variables? I don't want to have my controller to handle this business either.
Thanks
One thing you can do in this case it to use a partial, let's say views/_partials/storeList.blade.php
:
@foreach($store_listings as $fetch)
{{$title}} {{ $fetch->city }}, {{ $fetch->country }}
@endforeach
Then in your main view you just call it:
@include('_partials.storeList', array('title' => 'Website stores -'))
@include('_partials.storeList', array('title' => 'List of Stores in'))
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments