Pass multiple parameters to a blade directive

前端 未结 9 1743
时光说笑
时光说笑 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:49

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

提交回复
热议问题