Blade file not fully converting to PHP

Got a fatal error whilst developing a new page template, looked at the cached php file which sage is using and I see only half the file converts to <?php from @php

Fatal error: Uncaught Symfony\Component\Debug\Exception\FatalThrowableError: syntax error, unexpected '@' in /tmp/sage-cache/bd612976b245b29432910d0149089850b49fcd08.php on line *46*

The error starts at @else middle of the file…

Running PHP 7, no similar issues recently.

Hi @JordanC26, can you share your blade template with us? Without it, it’s hard to see what’s caused it.

Looking at the cache file, though, it looks like you are using the shorthand php directive @php(). If so, don’t use the shorthand.

Hi @codepuncher, that was it, thank you!

Still had this old shorthand php leftover, now updated.

{{-- Create active class if this is the first item in the loop --}}
@if ( $loop->first )
  @php( $active_status = 'active' )
@else
  @php( $active_status = '' )
@endif

Updated

{{-- Create active class if this is the first item in the loop --}}
@if ( $loop->first )
  @php
    $active_status = 'active';
  @endphp
@else
  @php
    $active_status = '';
  @endphp
@endif

You could also do this in a single line with ternary operators:

@php $active_status = $loop->first ? 'active' : '' @endphp
2 Likes

You don’t need to start & end PHP inside your @if / @else.

This topic was automatically closed after 42 days. New replies are no longer allowed.