Creating a directive with two parameters

Hi, I have a problem with creating a directive with two parameters where one is a variable

@php $icon = 'myIcon'; @endphp
@iconDir($icon, 'myClass')

because the first parameter for iconDir is a variable it doesn’t register in my directive

sage('blade')->compiler()->directive('iconDir', function ($expression) {
  eval("\$params = [$expression];");
  list($param1, $param2) = $params;
  return '$param1: ' . $param1 .'<br /> $param2: '. $param2 . '<br /> $expression: ' . "<?= {$expression}; ?>";
});

the output for this is imply

$param1:
$param2: myClass
$expression: myIconmyClass

Could someone please tell me how to solve this problem?

eval seems like a very dangerous way to accomplish this. How about something like:

$params = array_map('trim', explode(',' $expression));
1 Like

Thank you for the solution and pointing our the problem with eval

This topic was automatically closed after 42 days. New replies are no longer allowed.