php 检查laravel的blade指令中是否存在变量

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

Check if variable exist in laravel's blade directive

phplaravelundefinedlaravel-5.2directive

提问by pupadupa

I'm trying to create blade directive which echo variable (if variable defined) or echo "no data" if variable undefined.

我正在尝试创建刀片指令,该指令回显变量(如果定义了变量)或回显“无数据”(如果变量未定义)。

This is my code in AppServiceProvider.php:

这是我的代码AppServiceProvider.php

<?php

namespace App\Providers;

use Blade;
use Illuminate\Support\ServiceProvider;


class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Blade::directive('p', function($ex) {
            error_log(print_r($ex,true));
            return '<?php $defined_vars = get_defined_vars(); if(array_key_exists(\''. $ex .'\', $defined_vars) ): echo ' . $ex . ' ; else: echo \'no data\'; endif;?>';
        });
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

Here is my index.blade.php:

这是我的 index.blade.php:

<p class="lead">@p($myvar)</p>

But my directive "p" gives "no data" if variable defined. If I use isset error occurres: Cannot use isset() on the result of an expression (you can use "null !== expression" instead)

但是如果定义了变量,我的指令“p”给出“无数据”。如果我使用isset错误发生:Cannot use isset() on the result of an expression (you can use "null !== expression" instead)

How could I check inside directives if variable defined?

如果定义了变量,我如何检查内部指令?

回答by sanchezcl

Blade has a directive to check if a variable is set:

Blade 有一个指令来检查是否设置了变量:

@isset($var)

@endisset

回答by Ali

You can use in Blade functionality for checking isset i.e

您可以在 Blade 功能中使用来检查 isset,即

{{ $checkvariable or 'not-exist' }}

https://laravel.com/docs/5.2/blade#displaying-data

https://laravel.com/docs/5.2/blade#displaying-data

回答by Connor Leech

Try checking if the variable is empty:

尝试检查变量是否为空:

@if(empty($myvar))
    <p>Data does not exist</p>
@else
    <p>Your data is here!</p>
@endif

Can also check this thread

也可以检查这个线程

回答by George John

For Laravel 5.7 onwards use.

对于 Laravel 5.7 以后使用。

{{ $checkvariable ?? 'not-exist' }}

{{ $checkvariable ?? 'not-exist' }}

回答by TechPotter

The best and cleanest way check if a variable exists in blade:

检查刀片中是否存在变量的最佳和最干净的方法:

 {!! !empty($myvariable) ? $myvariable : 'variable does not exist' !!}

回答by Puneet Verma

For Laravel version >=5.7

对于 Laravel 版本 >=5.7

{{ $value ?? '' }}

For Laravel version <5.7

对于 Laravel 版本 <5.7

{{ $value or '' }}

回答by Jeemusu

What are you trying to pass to your custom directive? If it's just a string/int the following should work.

你想传递给你的自定义指令什么?如果它只是一个字符串/整数,则以下应该有效。

Blade::directive('p', function($expression){
    $output = $expression ? $expression : 'nodata';
    return "<?php echo {$output}; ?>";
});

In Blade Template

在刀片模板中

@p('Foo')

回答by crishoj

The @emptydirective might be useful:

@empty指令可能有用:

@empty($var)
   $var is unset or false-y
@endempty

回答by Dibyendu Mitra Roy

You can do it in few different ways.

您可以通过几种不同的方式来做到这一点。

Sample-1:

样本 1:

@if( !empty($data['var']))
   {{ $data['var'] }} 
@endif

Sample-2:

示例 2:

{{ $data['var'] or 'no data found' }}

Sample-3: Using ternary operator

示例 3:使用三元运算符

<a href="" class="{{ ( ! empty($data['var'] ? $data['var'] : 'no data found') }}">

回答by rickslayer

If you trying check a bool variable you can use @unless

如果您尝试检查 bool 变量,则可以使用 @unless

<input type="text" class="@unless ($variable) d-none @endunless" >

<input type="text" class="@unless ($variable) d-none @endunless" >