在Laravel blade中,您可以执行以下操作:
{{ $variable or 'default' }}
This will check if a variable is set or not. I get some data from the database, and those variables are always set, so I can not use this method.
我正在搜索一个用于执行此操作的速记"Blade "功能:
{{ ($variable != '' ? $variable : '') }}
很难用这段代码来做这件事,因为我不知道如何用链接或类似的东西来做:
<a href="{{ $school->website }}" target="_blank">{{ $school->website }}</a>
I tried:
{{ ($school->website != '' ? '<a href="{{ $school->website }}" target="_blank">{{ $school->website }}</a>' : '') }}
但是,它不起作用.而且,我希望我的代码尽可能简短;)
有人能给我解释一下吗?
UPDATE
I do not use a foreach because of, I get a single object (one school) from the database. I passed it from my controller to my view with:
$school = School::find($id);
return View::make('school.show')->with('school', $school);
So, I do not want to make an @if($value != ''){}
around each $variable (like $school->name).