Pass multiple parameters to a blade directive

前端 未结 9 1778
时光说笑
时光说笑 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 07:59

    I was searching for this exact solution, then decided to try something different after reading everything and ended up coming up with the solution you and I were both looking for.

    No need for JSON workarounds, explodes, associative arrays, etc... unless you want that functionality for something more complex later.

    Because Blade is just writing out PHP code to be interpreted later, whatever you've placed into your @highlight directive is the exact PHP code in string format that will be interpreted later.

    What to Do:

    Make and register a helper function that you can call throughout your application. Then use the helper function in your blade directive.

    Helper Definition:

    if(!function_exists('highlight')){
    
        function highlight($expression, $string){
            $expressionValues = preg_split('/\s+/', $expression);
    
            foreach ($expressionValues as $value) {
                $string = str_replace($value, "<b>".$value."</b>", $string);
            }
    
            return $string;
        }
    }
    

    Blade Directive:

    Blade::directive('highlight', function ($passedDirectiveString){
            return "<?php echo highlight($passedDirectiveString);?>";
        });
    

    Usage (Example):

    <div>
        @highlight('ho', 'house')
    </div>
    

    Understanding:

    This is equivalent to writing out:

    <div>
        {! highlight('ho', 'house') !}
    </div>
    
    0 讨论(0)
  • 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, "<b>".$value."</b>", $array['string']);
                }
    
                return "<?php echo {$array['string']}; ?>";
            });
        }
    
        public function register()
        {
        }
    }
    

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

    0 讨论(0)
  • 2021-02-19 08:01
    Blade::directive('custom', function ($expression) {
        eval("\$params = [$expression];");
        list($param1, $param2, $param3) = $params;
    
        // Great coding stuff here
    });
    

    and in blade template:

    @custom('param1', 'param2', 'param3')
    
    0 讨论(0)
提交回复
热议问题