Check if variable exist in laravel's blade directive

前端 未结 13 1438
清歌不尽
清歌不尽 2021-02-03 19:23

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.ph

13条回答
  •  失恋的感觉
    2021-02-03 19:29

    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 "";
    });
    

    In Blade Template

    @p('Foo')
    

提交回复
热议问题