2016-12-06 8 views
2

ブレード・ディレクティブを作成して、検索クエリから戻ってくるいくつかの単語を強調表示しようとしています。複数のパラメータをブレード・ディレクティブに渡す

これは私のブレードディレクティブです:

class AppServiceProvider extends ServiceProvider 

{ 
    public function boot() 
    { 
     Blade::directive('highlight', function($expression, $string){ 

      $expressionValues = preg_split('/\s+/', $expression); 

      foreach ($expressionValues as $value) { 
       $string = str_replace($value, "<b>".$value."</b>", $string); 
      } 

      return "<?php echo {$string}; ?>"; 
     }); 
    } 

    public function register() 
    { 
    } 
} 

そして、私はこのようなブレードに呼び出す:

@highlight('ho', 'house') 

しかし、このerrosは私に従っている:

Missing argument 2 for App\Providers\AppServiceProvider::App\Providers\{closure}() 

解決する方法それ?

答えて

0

私はあなたが1つのパラメータを渡すことができると思う。それはかなりありませんが、あなたはそうのような配列としてあなたのパラメータを渡すことができます。https://laracasts.com/discuss/channels/laravel/how-to-do-this-blade-directive

+0

エラー:

@highlight(['expression' => 'ho', 'string' => 'house']) 

は、だからあなたのディレクティブは

class AppServiceProvider extends ServiceProvider { public function boot() { Blade::directive('highlight', function($array){ $expressionValues = preg_split('/\s+/', $array['expression']); foreach ($expressionValues as $value) { $array['string'] = str_replace($value, "<b>".$value."</b>", $array['string']); } return "<?php echo {$array['string']}; ?>"; }); } public function register() { } } 

はここでそれを見つけることができ '不正な文字列オフセット '式' –

0
Blade::directive('highlight', function($arguments){ 

     list($arg1, $arg2) = explode(',',str_replace(['(',')',' ', "'"], '', $arguments)); 

     $expressionValues = preg_split('/\s+/', $arg1); 

     $output = ""; 

     foreach ($expressionValues as $value) { 
      $output .= str_replace($value, "<b>".$value."</b>", $arg2); 
     } 

     return "<?php echo \"{$output}\"; ?>"; 
    }); 
関連する問題