Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 Blade Custom Directive - trouble passing variable

I am trying to create a Blade directive that will set a class on my table row depending on what gets passed in. The problem is that when I call it, the value of the variable is not being passed in - the literal string that I have between the parenthesis is.

In my view:

@row($inspection->inspection_disposition)

In the directive:

 Blade::directive('row', function($data)
    {
        var_dump($data);
           .
           .
           .
     }

What I see in the dump is:

string(37) "($inspection->inspection_disposition)"

Shouldn't I see the value of that variable? That's what I want. What am I missing here?

MORE INFO:

I need to use the value of that variable in the directive, like this:

if($data == "hello")
{
   return something
}
elseif($data == "goodbye")
{
   return something else
}

This is a simplified example, but hopefully it will help to illustrate that I need to compare the value of the variable inside the directive, then determine what to do. Perhaps I need to use eval() ?

like image 729
Kenny Avatar asked Oct 19 '22 12:10

Kenny


1 Answers

Blade directive should return a string that php will interprete, like this:

Blade::directive('row', function($data)
{
    return "<?php var_dump($data); ?>";
}
like image 132
RDev Avatar answered Oct 21 '22 05:10

RDev