多个@foreach的观点 - Laravel

时间:2014-03-11 14:20:36

标签: php laravel laravel-4 blade

我需要在我的刀片模板中输出各种@foreach循环,但是它们都在我的数据库中定位同一个表,只是获取不同的字段。作为一个例子,我在我的主要布局中使用@yield,在标题/描述标签等的视图中使用@section:

@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

然后在layout.main中:

<title>@yield('title')</title>

<meta name="description" content="@yield('description')" />

这个重复的@foreach循环出现在我在其他页面上的视图的其他部分中。我试着通过DRY方法工作。当我想做的就是调用一些变量时,这里有什么建议来保存我进入多个foreach循环?我也不想让我的控制器来处理这项业务。

由于

1 个答案:

答案 0 :(得分:1)

在这种情况下你可以做的一件事是使用部分,让我们说views/_partials/storeList.blade.php

@foreach($store_listings as $fetch)
   {{$title}} {{ $fetch->city }}, {{ $fetch->country }}
@endforeach

然后在主视图中,您只需将其命名为:

@include('_partials.storeList', array('title' => 'Website stores -'))

@include('_partials.storeList', array('title' => 'List of Stores in'))
相关问题