Pass multiple parameters to a blade directive

前端 未结 9 1742
时光说笑
时光说笑 2021-02-19 07:31

I\'m trying to create a blade directive to highlight some words that will return from my search query.

This is my blade directive:

class AppServiceProvid         


        
9条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-19 08:00

    I think you can only pass one parameter. It's not pretty but you could pass your parameters as an array like so:

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

    So your directive could be

    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, "".$value."", $array['string']);
                }
    
                return "";
            });
        }
    
        public function register()
        {
        }
    }
    

    Found it here: https://laracasts.com/discuss/channels/laravel/how-to-do-this-blade-directive

提交回复
热议问题