php - Check if variable exist in laravel's blade directive -


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

this code in appserviceprovider.php:

<?php  namespace app\providers;  use blade; use illuminate\support\serviceprovider;   class appserviceprovider extends serviceprovider {     /**      * bootstrap 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 application services.      *      * @return void      */     public function register()     {         //     } } 

here index.blade.php:

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

but directive "p" gives "no data" if variable defined. if use isset error occurres: cannot use isset() on result of expression (you can use "null !== expression" instead)

how check inside directives if variable defined?

you can use in blade functionality checking isset i.e

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

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


Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -