0

I want to disable a link by adding a class and setting its href.

I came up with this but I am not happy with it.

    <a class="linkclass {!! $disabled ? 'disabled' : '" href="/page' !!}">Link</a>

So my questions are:

  1. Is this a good practice? Do you recommend setting href by its condition of being disabled?
  2. Which is the way you would suggest to do that?
3
  • why not just do href={{$link or '#'}}? Commented Mar 2, 2017 at 13:10
  • What about the class? I want to set the class to disabled if the href is #. Commented Mar 2, 2017 at 14:08
  • I want to avoid the duplicity: if the link is active has href and no 'disabled' class, if it is disabled just the opposite. Taking this into account, is there any single condition solution? Commented Mar 2, 2017 at 14:22

1 Answer 1

1

if $disabled is a bool

@if($disabled)
    <a class="linkclass disabled">Link</a> // or optional <a class="linkclass disabled" href="#">Link</a>
@else
    <a class="linkclass" href="/link">Link</a>
@endif

but maybe it's better to have $disabled also contain the class string value "disabled" or empty.

<a class="linkclass {{$disabled}}"href="@if(isset($disabled))#@else /link @endif"></a>
Sign up to request clarification or add additional context in comments.

3 Comments

Ok @ThomasMoors, thanks for your answer, I assume maybe there is not a single condition solution. I suggest using ternary operator for the second solution <a class="linkclass {{$disabled}}" href="{{isset($disabled) ? '#' : '/link'}}"></a>
Why is this so bad in your opinion? This is at least escaped already, which is more secure. It also does not add a notable performance decrease at all, so what is the problem you have with this solution?
I don't think this is bad. Maybe it is a bad question! I was asking for a all-in-one solution like my first code.

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.