php - Laravel Blade - User Allowed to Input Variable -
i'm making admin setting section of laravel 5.2 app using storage package thetispro/laravel5-setting.
i'd admin users able update email copy sent out user, of emails include variables such users name. "thanks shopping us, customer name".
i can store following in setting, when blade outputs it prints out string instead of variable. i've tried escaped , nonescaped characters {{}} , {{!! !!}. here's have:
email message admin user can edit:
<h2>hi, {{ $user->name }}</h2> <p>welcome web app</p> in view have:
{!! setting::get('emailuserinvite') !!} <br /><br /> <!-- testing both escaped , nonescaped versions --> {{ setting::get('emailuserinvite') }} what blade renders just:
echo "<h2>hi, {{ $user->name }}</h2> <p>welcome web app</p>"; i trying make custom blade directive close echo, display variable , open echo up, doesn't seem working correctly either.
// appserviceprovider blade::directive('echobreak', function ($expression) {   // echo "my string " . $var . " close string";   $var = $expression;   return "' . $var . '"; });  // admin user settings hi @echobreak($user->name) welcome web app any advice appreciated! thanks.
update
i mocked simple test case using @abdou-tahiri's example i'm still getting errors eval()'d code.
errorexception in settingcontroller.php(26) : eval()'d code line 1:   undefined variable: user and here simple controller:
<?php  namespace app\http\controllers;  use illuminate\http\request;  use app\http\requests; use blade;  class settingcontroller extends controller {      public function index() {         $user = [             "fname" => "sam",             "lname" => "yerkes"];         $str = '{{ $user }}';         return $this->bladecompile($str, $user);     }      private function bladecompile($value, array $args = [])     {         $generated = \blade::compilestring($value);         ob_start() , extract($args, extr_skip);         try {             eval('?>'.$generated);         }          catch (\exception $e) {             ob_get_clean(); throw $e;         }         $content = ob_get_clean();         return $content;     }  } 
you may need compile string using blade , check helper function :
function blade_compile($value, array $args = array()) {     $generated = \blade::compilestring($value);      ob_start() , extract($args, extr_skip);      // we'll include view contents parsing within catcher     // can avoid wsod errors. if exception occurs     // throw out exception handler.     try     {         eval('?>'.$generated);     }      // if caught exception, we'll silently flush output     // buffer no partially rendered views thrown out     // client , confuse user junk.     catch (\exception $e)     {         ob_get_clean(); throw $e;     }      $content = ob_get_clean();      return $content; } so in view file :
{!! blade_compile(setting::get('emailuserinvite'),compact('user')) !!} 
Comments
Post a Comment