php Laravel 4.2 刀片:检查是否为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27486179/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:24:03  来源:igfitidea点击:

Laravel 4.2 blade: check if empty

phplaravelblade

提问by Marten

In Laravel blade you can do:

在 Laravel 刀片中,您可以执行以下操作:

{{ $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.

这将检查是否设置了变量。我从数据库中获取了一些数据,并且这些变量总是被设置的,所以我不能使用这种方法。

I am searching for a shorthand 'blade' function for doing this:

我正在寻找用于执行此操作的速记“刀片”功能:

{{ ($variable != '' ? $variable : '') }}

It is hard to use this piece or code for doing this beacuse of, I do not know how to do it with a link or something like this:

很难使用这篇文章或代码来做这件事,因为我不知道如何用链接或类似的东西来做:

<a href="{{ $school->website }}" target="_blank">{{ $school->website }}</a>

I tried:

我试过:

{{ ($school->website != '' ? '<a href="{{ $school->website }}" target="_blank">{{ $school->website }}</a>' : '') }}

But, it does not work. And, I would like to keep my code as short as possible ;)

但是,它不起作用。而且,我想让我的代码尽可能短;)

Can someone explain it to me?

有人可以向我解释一下吗?

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:

我不使用 foreach,因为我从数据库中获得了一个对象(一所学校)。我将它从我的控制器传递到我的视图:

 $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).

所以,我不想@if($value != ''){}围绕每个 $variable (如 $school->name)。

采纳答案by soroush gholamzadeh

try this:

尝试这个:

@if ($value !== '')
    {{ HTML::link($value,'some text') }}
@endif

回答by Jeff Puckett

I prefer the @unlessdirectivefor readability in this circumstance.

在这种情况下,我更喜欢可读性@unless指令

@unless ( empty($school->website) )
    <a href="{{ $school->website }}" target="_blank">{{ $school->website }}</a>
@endunless

回答by Rajitha Bandara

With php 7, you can use null coalescing operator. This is a shorthand for @m0z4rt's answer.

使用 php 7,您可以使用空合并运算符。这是@m0z4rt 答案的简写。

{{ $variable ?? 'default' }}

回答by lieroes

{{ ($school->website != '' ? '<a href="{{ $school->website }}" target="_blank">{{ $school->website }}</a>' : '') }}

change to

改成

{{ ($school->website != '') ? '<a href="' . $school->website . '" target="_blank">' .  $school->website . '</a>' : '' }}

or the same code

或相同的代码

{{ ($school->website != '') ? "<a href='$school->website' target='_blank'>$school->website</a>" : '' }}

回答by m0z4rt

{{ isset($variable) ? $variable : 'default' }}

回答by R. Gurung

I wonder why nobody talked about $variable->isEmpty()it looks more better than other. Can be used like:

我想知道为什么没有人谈论$variable->isEmpty()它看起来比其他人更好。可以像这样使用:

@if($var->isEmpty())
    Do this  
@else
    Do that  
@endif