在具有相同 parent 布局的 @each 指令中使用不同的 child 视图
Using different child views in @each directive with the same parent layout
我在 Laravel 5.3 中使用 Blade 个模板。我想呈现两个列表 - 'friends' 之一和 'acquaintances' 之一。两种情况下列表的页眉和页脚都相同,但朋友列表中呈现的项目与熟人列表中呈现的项目具有不同的格式和字段。
我的控制器中有两个方法:
public function showFriends() {
return view('reports.friends', ['profiles' => $friends]);
}
public function showAcquaintances() {
return view('reports.acquaintances', ['profiles' => $acquaintances']);
}
这是我的 blade 模板:
// reports/acquaintances.blade.php
<div>Some generic header HTML</div>
<div class="container">
@each('reports.acquaintance', $profiles, 'profile')
</div>
<div>Some generic footer HTML</div>
// reports/acquaintance.blade.php
<div class="media">
<div>Some HTML formatting specific to acquaintance item</div>
{{ $profile->name }}
{{ $profile->job }}
</div>
// reports/friends.blade.php
<div>Some generic header HTML</div>
<div class="container">
@each('reports.friend', $profile, 'profile')
</div>
<div>Some generic footer HTML</div>
// reports/friend.blade.php
<div class="media">
<div>Some HTML formatting specific to friend item</div>
{{ $profile->name }}
{{ $profile->birthday }}
</div>
这似乎不是实现我想要的效果的非常有效的方法,因为我不得不为我的列表创建两个相同的 parent 模板:friends.blade.php 和 acquaintances.blade.php.我真正需要的是能够拥有一个通用的 parent 模板,然后以某种方式在我的控制器中指定我想使用哪个模板来呈现列表项。这可能吗?还有另一种更优雅的方法来实现吗?我刚刚开始了解 Blade,非常感谢任何指点。
您可以将其分解为一个通用 persons_list
和两个自定义项目。然后在列表内部使用条件:
public function showFriends() {
return view('reports.persons_list', ['profiles' => $friends, 'type' => 'friends']);
}
public function showAcquaintances() {
return view('reports.persons_list', ['profiles' => $acquaintances, 'type' => 'acquaintances']);
}
和blade:
// reports/persons_list.blade.php
<div>Some generic header HTML</div>
<div class="container">
@if ($type == 'friends')
@each('reports.friend', $profiles, 'profile')
@else
@each('reports.acquaintance', $profiles, 'profile')
@endif
</div>
<div>Some generic footer HTML</div>
我在 Laravel 5.3 中使用 Blade 个模板。我想呈现两个列表 - 'friends' 之一和 'acquaintances' 之一。两种情况下列表的页眉和页脚都相同,但朋友列表中呈现的项目与熟人列表中呈现的项目具有不同的格式和字段。
我的控制器中有两个方法:
public function showFriends() {
return view('reports.friends', ['profiles' => $friends]);
}
public function showAcquaintances() {
return view('reports.acquaintances', ['profiles' => $acquaintances']);
}
这是我的 blade 模板:
// reports/acquaintances.blade.php
<div>Some generic header HTML</div>
<div class="container">
@each('reports.acquaintance', $profiles, 'profile')
</div>
<div>Some generic footer HTML</div>
// reports/acquaintance.blade.php
<div class="media">
<div>Some HTML formatting specific to acquaintance item</div>
{{ $profile->name }}
{{ $profile->job }}
</div>
// reports/friends.blade.php
<div>Some generic header HTML</div>
<div class="container">
@each('reports.friend', $profile, 'profile')
</div>
<div>Some generic footer HTML</div>
// reports/friend.blade.php
<div class="media">
<div>Some HTML formatting specific to friend item</div>
{{ $profile->name }}
{{ $profile->birthday }}
</div>
这似乎不是实现我想要的效果的非常有效的方法,因为我不得不为我的列表创建两个相同的 parent 模板:friends.blade.php 和 acquaintances.blade.php.我真正需要的是能够拥有一个通用的 parent 模板,然后以某种方式在我的控制器中指定我想使用哪个模板来呈现列表项。这可能吗?还有另一种更优雅的方法来实现吗?我刚刚开始了解 Blade,非常感谢任何指点。
您可以将其分解为一个通用 persons_list
和两个自定义项目。然后在列表内部使用条件:
public function showFriends() {
return view('reports.persons_list', ['profiles' => $friends, 'type' => 'friends']);
}
public function showAcquaintances() {
return view('reports.persons_list', ['profiles' => $acquaintances, 'type' => 'acquaintances']);
}
和blade:
// reports/persons_list.blade.php
<div>Some generic header HTML</div>
<div class="container">
@if ($type == 'friends')
@each('reports.friend', $profiles, 'profile')
@else
@each('reports.acquaintance', $profiles, 'profile')
@endif
</div>
<div>Some generic footer HTML</div>