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' }}
Comments
Post a Comment