Sage 9: Parse error: syntax error, unexpected '@' Blade Template

Parse error: syntax error, unexpected '@' line 4

<!doctype html>
<html <?php(language_attributes())>
@include('partials.head')
<body @php(body_class())>

I am calling this and not including layouts.app on a custom page, because I want to have a different header then on the other pages. I suddenly get this error without changing anything. Seems like my blade templates don’t build. Any hints?

That is your blade template. The error should be coming from the compiled template in uploads\cache folder. What does line 4 say in that file?

Yes sorry, didn’t mention this: This code is actually copied from the .php file in the uploads/cache folder.

Looks like you have an error in the first php tag. Try:

<html @php(language_attributes())>

You’re missing the closing php tag in your html tag!
change it to

<html <?php language_attributes() ?>>

or the blade version @KimH suggested.

This is what I have in my blade template file:

<!doctype html>
<html @php(language_attributes())>
@include('partials.head')
<body @php(body_class())>

And this is what I got in the file from the uploads/cache folder where the error is thrown.

<!doctype html>
<html <?php(language_attributes())>
@include('partials.head')
<body @php(body_class())>

So I was already using @KimH 's suggestion.

You need to use @endphp when you use @php. I think this was a relatively recent change in Laravel - see the ticket here:

So you can either add the @endphpor switch to using plain <?php ?> tags in your template…

Thanks @Stephen that worked for me.

Just not sure why this works, because on other template files there is the @php() syntax without @endphp without any errors :face_with_monocle:

It’s possible those templates were compiled with an earlier version of Blade before this change was made and therefore they are still ok (I don’t think they should be recompiled unless the .blade.php file is updated).

You could test that theory by making a simple change to one of the working template files and see if it ends up broken suddenly.

I had to make a custom blade directive some time ago to get this to work, always assumed it was a bug. Something like @run( %codehere% ), don’t remember the detail but it worked like the old @php().

@Stephen That issue is for version 5.5. Sage is using 5.4. I am wondering why this is actually happening.

At any rate, blocks of @php ... @endphp code look nice, especially if they are conditionals or loops. And as some have mentioned, stick with the <?php something(); ?> for inline code. I even like to echo with <?= $variable ?> just out of habit.

@Webstractions - good catch! I don’t know what would be causing it then… It seems that it was never the intended behaviour to work without @endphp so maybe in some setups, it causes the a different result. It’s hard to say without digging into it a lot deeper.

I’ll stick to the full syntax if I need PHP. For variables, I prefer the Blade {{ $variable }} syntax - it’s just that bit faster to type and you get the HTML escaping thrown in as part of it.