If/else at the end of a blade file throw exception Hello,
Blade is throwing an exception when I try having an if/else statement at the end of a file for closing a specific tag. Would anyone know why?
@if (auth()->user())
</x-app-layout >
@else
</x-guest-layout>
@endif
I get: syntax error, unexpected token "else"
I have the same if/else at the top of the file for opening the proper tag, and it works.
Trying removing the space:
</x-app-layout > <!-- here -->
</x-app-layout>
@tykus I missed that, but the exception is still thrown
@Romain is that the offending code - can you get past the error message if it was removed (albeit the closing tag is missing obviously)?
Can you show the complete file? I just tested it, and it seems to work fine here.
@tykus that's the end of the file. It's the newest addition to this file. At the top I have the same code for the opening tags:
@if (auth()->user())
<x-app-layout>
@else
<x-guest-layout>
@endif
And that doesn't throw an error
@Romain So if you only have this, it still fails? Or is the error somewhere between the two
@if (auth()->user())
<x-app-layout>
@else
<x-guest-layout>
@endif
lalala
@if (auth()->user())
</x-app-layou >
@else
</x-guest-layout>
@endif
@Romain the problem here is the fact that you have Blade component tags inside the conditional blocks. You could instead use the @component Blade directive:
@if (auth()->user())
@component('components.app-layout')
@else
@component('components.guest-layout')
@endif
<!-- Rest of document -->
@endcomponent
@tykus hmm if that's really the issue then I will rethink this logic.
I'll try something else and get back to you guys
You're adding the component closing tag without the opening tag.
@if (auth()->user())
<x-app-layout ></x-app-layout >
@else
<x-guest-layout></x-guest-layout>
@endif
OR
@if (auth()->user())
<x-app-layout />
@else
<x-guest-layout />
@endif
Please sign in or create an account to participate in this conversation.