allegjdm93
allegjdm93

Reputation: 602

How to use custom blade directive in Laravel

I am trying to use the argument passed in the directive to evaluate a condition and returned a string like this

Blade::directive('resource', function($page) {
    return (strcmp(URL::full(), URL::to("/admin/" + $page)) == 0) ? "active" : "inactive";
});

If the page name passed in evaluates to the full url then return the active string, otherwise return inactive. For styling purposes

The problem is that $page evaluates to (value) where value is the string passed in. Am I able to use this argument to do calculation on conditions here?

The closest post I could find is Using a Blade directive in a Blade directive but no answer.

Thank you

Upvotes: 4

Views: 2225

Answers (1)

Absalon Valdes
Absalon Valdes

Reputation: 910

Try

Blade::directive('resource', function($page) {
    return "<?php echo '".((strcmp(URL::full(), URL::to("/admin/" + $page)) == 0) ? "active" : "inactive")."' ?>"
});

Upvotes: 1

Related Questions