I have some frequently used complex UI elements built with HTML (such as cards, button groups, etc..) For this purpose, I would like to create reusable blade elements.
The closest solution I found is custom blade directives.
I defined a simple card directive as follows:
Blade::directive('card', function ($expression) {
list($title, $icon, $buttons) = explode(',', str_replace(["'"], '', $expression));
$str = '<div class="card card-custom">
<div class="card-header">
<div class="card-title">
<span class="card-icon">
<i class="'.$icon.'"></i>
</span>
<h3 class="card-label">'.$title.'
</div>
<div class="card-toolbar">'.$buttons.'
</div>
</div>
<div class="card-body">';
return "<?php echo '".$str."'; ? >";
});
Blade::directive('endcard', function () {
return '</div></div>';
})
and in my blade I just call it like this:
@card('Dashboard', 'fa fa-tachometer', $buttonsHtml)
<div> Card content here...</div>
@endcard
Which would like the attached image.
The problem is that I can't pass any parameter as variable (like $buttonsHtml) because it will take it literally as "$buttonsHtml".
I haven't seen anyone one trying to evaluate the variable before passing it to the custom directive. Is it even possible to do that?
It would be useless if I need to pass the actual value of the $buttonsHtml variable since it is being generated by some other function. Any one has an idea of a work around, or even a substitution to using custom directives?
