laravel 4 - 如何在刀片中显示和隐藏组件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23493249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
laravel 4 - how to show and hide components in blade?
提问by Brightside
I have breadcrumbs in my master.blade.php file and i want the breadcrumbs to be used everywhere BUT the homepage.blade.php.
我的 master.blade.php 文件中有面包屑,我希望面包屑可以在除 homepage.blade.php 之外的任何地方使用。
right now this is how i add links to the breadcrumbs in other pages like "About". about.blade.php:
现在这就是我在其他页面(如“关于”)中添加指向面包屑的链接的方式。关于.blade.php:
@section('breadcrumbs')
@parent
<li class="last-breadcrumb">
About
</li>
@stop
in the master.blade.php:
在 master.blade.php 中:
<div class="container">
<div class="breadcrumb-container">
<ul class="breadcrumb">
@section('breadcrumbs')
<li>
<a href="/homepage/" title="homepage">
homepage
</a>
</li>
@show
</ul>
</div>
</div>
but i don't want the breadcrumbs code to display at all when homepage.blade been used.
但我不希望在使用 homepage.blade 时显示面包屑代码。
copying the code for each about.blade/X.blade files seems like a bad idea..
复制每个 about.blade/X.blade 文件的代码似乎是个坏主意..
采纳答案by sidneydobber
Your can set a value in your controller that you pass with the make/redirect like $data['breadcrumb'] = true
and wrap your breadcrumb code in an conditional. This kind of system also works well for error and success messages since you can alse send content from your controller. Then you would send an array with values instead of true
.
您可以在您的控制器中设置一个值,您可以使用 make/redirect 之类的方式传递该值,$data['breadcrumb'] = true
并将您的面包屑代码包装在条件中。这种系统也适用于错误和成功消息,因为您还可以从控制器发送内容。然后你会发送一个带有值的数组而不是true
.
Blade template
刀片模板
<div class="container">
@if($breadcrumb)
<div class="breadcrumb-container">
<ul class="breadcrumb">
@section('breadcrumbs')
<li>
<a href="/homepage/" title="homepage">
homepage
</a>
</li>
@show
</ul>
</div>
@endif
</div>
回答by user1669496
You can check the URI to see if you want to display it or not. It's logic in your view which is usually frowned upon, but it's the easiest way I can come up with.
您可以检查 URI 以查看是否要显示它。在你看来,这是通常不受欢迎的逻辑,但这是我能想出的最简单的方法。
<div class="container">
<div class="breadcrumb-container">
<ul class="breadcrumb">
@if(Route::uri() == '/')
<li class="last-breadcrumb">
About
</li>
@endif
<li>
<a href="/homepage/" title="homepage">
homepage
</a>
</li>
@show
</ul>
</div>
</div>
Depending on the URI you are using for your home page, you may need to tweak the condition.
根据您用于主页的 URI,您可能需要调整条件。
回答by edcs
If you use @yield
in your master template then it will only display content when the section has been defined. @content
requires the section to be always defined.
如果您@yield
在主模板中使用,那么它只会在定义该部分时显示内容。@content
要求始终定义该部分。