php Laravel Blade - 如果满足条件则添加一个类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23382907/
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 blade - Add a class if a condition is met
提问by Elaine Marley
This is simple but I can't seem to be able to do it with blade and I usually go back to php for this but, since I can't declare a new php variable with blade (without printing it, that is) how should I change a simple class name if a certain condition is met? Take this example of a link, I just want to add the class "active" if $hostess->active=1
这很简单,但我似乎无法使用刀片来做到这一点,我通常会为此返回 php,但是,因为我无法使用刀片声明一个新的 php 变量(即不打印它)应该如何如果满足某个条件,我会更改一个简单的类名吗?以这个链接为例,如果 $hostess->active=1,我只想添加类“active”
{{ HTML::linkRoute('hostesses.index', 'Archived', $params, array('class' => 'btn btn-default')) }}
I know how to do this with php, but how would it be done with blade?
我知道如何用 php 做到这一点,但如何用 Blade 做到这一点?
采纳答案by The Alpha
Something like this?
像这样的东西?
{{ HTML::linkRoute('hostesses.index', 'Archived', $params, array('class' => $hostess->active ? 'btn btn-info' : 'btn btn-default')) }}
回答by JP Blanco
You could do this:
你可以这样做:
// App/Http/routes.php
Route::get('foo/bar', 'FooController@bar);
// Your blade file
<li class="{{ Request::path() == 'foo/bar' ? 'active' : '' }}">
<a href="{{ url('foo/bar') }}"></i> Bar</a>
</li>