Pass multiple parameters to a blade directive

橙三吉。 提交于 2019-12-21 09:38:14

问题


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 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()
    {
    }
}

And I call in blade like this:

@highlight('ho', 'house')

But, this erros is following me:

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

How to solve it?


回答1:


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')



回答2:


For associative arrays, eval() may be the easiest. But its use is adverted as dangerous, because it's like your opening a hole, a needle for code execution. In same time eval() execute at runtime, well it store the code to be executed in database (caching [well it mean it cache compiled byte code]). That's additional overhead, so performance will take a hit. Here's a nice paper on the topic [didn't read or get into the details]) https://link.springer.com/chapter/10.1007%2F978-981-10-3935-5_12.

Well here I may have got you!, there is no performance difference at server serving performance, because views are cached, and generated only when you change them. Directives are translated to php code and in another process they are cached. (you can find the generated view in storage/framework/views)

So for

Blade::directive('custom', function ($expression) {
    eval("\$myarray = [$expression];");

    // do something with $myarray
    return "<?php echo ..";
});

It's just ok. There is nothing to talk about for eval() and performance (it's done and cached, and the generated php code is the one that will run over and over (just make sure the returned php code by the directive doesn't hold eval(), unless there is a reason). Using eval() directly (which will be used for different request over and over) will impact performance. (I wanted to talk about eval(), I think those are useful info)

as it is we can parse array form ["sometin" => "i should be sting", "" => "", ...].

eval("\$array = $expression;");
// then we can do what we want with $array 

However we can't pass variables. ex: @directive(["s" => $var]) if we use eval, $var will be undefined in the directive function scope. (don't forget that directive are just a way to generate tempalte beautifully, and turning the ugly (not really ugly) php code into such directive. In fact it's the inverse, we are turning the beautiful directive to the php code that will be executed at the end. And all you are doing here is generating, building, writing the expression that will form the final php pages or files.)

What you can do instead is to pass the variable in this way ["s" => "$var"] , so it will pass through eval. And then in your return statement, use it example:

return "<?php echo ".$array['s'].";?>";

when the template will be generated this will be <?php echo $var;?>;

Remember, if you decide to use eval, never use it within the returned string! or maybe you want to in some cases.

Another solution

(which is easy) along to the proposed parsing solutions, is to use a json format to passe data to your directive, and just use json_decode. (it just came to me)

class AppServiceProvider extends ServiceProvider

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

            $myArray = json_decode($json_expression)

            // do something with the array
        });
    }

    public function register()
    {
    }
}

Here an example where I needed to do so: the goal is to automate this

@php
    $logo = !empty($logo) ? $logo : 'logo';
    $width = !empty($width) ? $width : 'logo';
    //...    // wait i will not always keep doing that ! h h
@endphp // imaging we do that for all different number of view components ...

and so I wrote this directive:

 public function boot()
    {
        Blade::directive('varSet', function ($expr) {
            $array = json_decode($expr, true);

            $p = '<?php ';
            foreach ($array as $key => $val) {
                if (is_string($val)) {
                    $p .= "\$$key = isset(\$$key) && !empty(\$$key) ? \$$key : '$val'; ";
                } else {
                    $p .= "\$$key = isset(\$$key) && !empty(\$$key) ? \$$key : $val; ";
                }
            }
            $p .= '?>';

            return $p;
        });
    }

We use it like this:

@varSet({
    "logo": "logo",
    "width": 78,
    "height": 22
})// hi my cool directive. that's slick.

Why this form work? it get passed as a string template like this

"""
{\n
    "logo": "logo",\n
    "width": 78,\n
    "height": 22\n
}
"""

For using in template variable pass them as string like that "$var", same as what we did with eval.

For parsing from ["" => "", ..] format may be eval() is the best choice. Remember that this is done at template generation which are cached later, and not updated, until we make change again. And remember to not use eval() within the return ; directive instruction. (only if your application need that)

for just multi arguments, and so not an array: A function like that will do the job:

 public static function parseMultipleArgs($expression)
{
    return collect(explode(',', $expression))->map(function ($item) {
        return trim($item);
    });
}

or

public static function parseMultipleArgs($expression)
    {
        $ar = explode(',', $expression);
        $l = len($ar);

        if($l == 1) return $ar[0];

        for($i = 0; $i < $l; $i++){$ar[$i] = trim($ar[$i])}

        return $ar;
    }

and you can tweak them as you like, using str_replace to remove things like () ...etc [in short we workout our own parsing. RegEx can be helpful. And depend on what we want to achieve.

All the above are way to parse entries and separate them into variables you use for generating the template. And so for making your return statement.

WHAT IF ALL YOU WANT IS TO HAVE YOUR DIRECTIVE TAKE AN ARRAY WITH VARIABLES FROM THE VIEW SCOPE:

like in @section('', ["var" => $varValue])

Well here particulary we use the multi arguments parsing, then we recover ["" => ..] expression separately (and here is not the point).

The point is when you want to pass an array to be used in your code (view scope). You just use it as it is. (it can be confusing).

ex:

Blade::directive("do", function ($expr) {
    return "<?php someFunctionFromMyGlobalOrViewScopThatTakeArrayAsParameter($expr); ?>
});

This will evaluate to

<?php someFunctionFromMyGlobalOrViewScopThatTakeArrayAsParameter(["name" => $user->name, .......]); ?>

And so all will work all right. I took an example where we use a function, you can put all a logic. Directives are just a way to write view in a more beautiful way. Also it allow for pre-view processing and generation. Quiet nice.




回答3:


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




回答4:


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>



回答5:


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}\"; ?>";
    });



回答6:


The value received on blade directive function is a sting, so, you must parse to get the values:

BLADE

@date($date, 'd-M-Y')

AppServiceProvider

Blade::directive('date', function ($str) {
  // $str = "$date, 'd-M-Y'";
  $data = explode(',',str_replace(' ', '', $str));
  //$data = ["$date", "'d-M-Y'"]
  $date = $data[0];
  $format = $data[1];
  return "<?= date_format(date_create($date), $format) ?>";
});



回答7:


If you want to reference variables within a custom blade directive you may not need to pass them directly to the directive. I solved this problem by calling the blade directive from within a blade component. Blade components have local variable scope and so you can simply pass all the variables you need within the call to the blade component (without polluting your view scope). This is sufficient so long as you don't actually need to modify the variables or use them for control logic in your directive.

//view.blade.php
@component('my-component',['myVar1'=> $something, 'myVar2'=>$somethingElse])
@endcomponent

//my-component.blade.php
@myBladeDirective('Two variables accessible')

//Boot method of relevant service provider
Blade::directive('myBladeDirective', function ($someVar) {
    return "<?php echo $someVar : {$myVar1} and {$myVar2};?>
});



回答8:


I found an alternative approach to accessing View variables within a Blade Directive.

I wanted to check whether a given string appeared as an array key in a variable accessible in the view scope.

As the Blade Directive returns PHP which is evaluated later, it is possible to 'trick' the Directive by breaking up a variable name so that it doesn't try to parse it.

For example:

    Blade::directive('getElementProps', function ($elementID) {
        return "<?php
            // Reference the $elementData variable
            // by splitting its name so it's not parsed here
            if (array_key_exists($elementID, $" . "elementData)) {
                echo $" . "elementData[$elementID];
            }
        ?>";
    }); 

In this example we have split the $elementData variable name so the Blade Directive treats it like a spring. When the concatenated string is returned to the blade it will be evaluated as the variable.



来源:https://stackoverflow.com/questions/41003733/pass-multiple-parameters-to-a-blade-directive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!