0

I want to print a button in my Laravel (blade) views. I have made a function for creating this button, because of: if the user, who loads the view, doesn't have rights to see this button, the button won't show up (I haven't made this part yet). In every view where I need a button for something, I want to use this method. The return value of the method is:

<a href="{{ URL::to(scholen/toevoegen) }}"><button class="btn btn-primary">Nieuwe school</button></a>

There is just a problem: the method createButton() returns a string. So, {{ URL::to('scholen/toevoegen') }} doesn't work in blade.

Is there a solution for this? I don't want to type the url in my method, becuase of it has to be variable.

View

<?= Authorize::createButton('Nieuwe school', array('scholen', 'toevoegen'), null, 'btn btn-primary'); ?>

Helper Class

static function createButton($text = null, $route = null, $id = null, $class = null)
{

        $button = '<a href="{{ URL::to(<!URL!>) }}"><button class="<!CLASS!>"><!TEXT!></button></a>';

        if ($text != null) {
            $button = str_replace("<!TEXT!>", $text, $button);
        } 

        if ($route[0] != null && $route[1] != null) {
            $button = str_replace("<!URL!>", $route[0] .'/'. $route[1] . ($id != null ? '/'. $id : ''), $button);
        } 

        if ($class != null) {
            $button = str_replace("<!CLASS!>", $class, $button);
        }

        return $button;
}

1 Answer 1

1

I would suggest evaluated the route variables within the createButton() method, rather than in Blade - like this:

$button = '<a href="' . URL::to($someConstructedRoute) . '"><button class="<!CLASS!>"><!TEXT!></button></a>';

Now, all we need is to create the constructed route above that code.

However, I also think there's considerable room for improvement in your method with e.g. sprintf(). It also seems that the route should never actually be empty, because you always want a real link. Here's an example of how I might change it - but it's just a suggestion.

static function createButton($routeVars, $text = '', $id = null, $class = '')
{
    $route = implode('/', $routeVars);
    if ($id) $route .= '/' . $id;
    $url = URL::to($route);

    return sprintf('<a href="%s"><button class="%s">%s</button></a>', 
        $url, $class, $text
    );
}

By changing the nulls to empty strings, we can just include them as is. If there's no text provided, for instance, the button will look like <a href="somelink"><button class="someclass"></button></a> with no text.

Actually, I would probably extract the three route creation to a static method as well, but that might be a bit nitpicky.

Hope this helps. :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.