Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Single @php Blade directive does not work correctly when followed by a @endphp tag #47948

Closed
marcbelletre opened this issue Aug 3, 2023 · 3 comments

Comments

@marcbelletre
Copy link

marcbelletre commented Aug 3, 2023

Laravel Version

10.17.1

PHP Version

8.2.7

Database Driver & Version

No response

Description

The documentation states that we can use a single @php directive like this:

@php($counter = 1)

It works fine until there is another block of raw PHP that uses the full syntax after.

@php($index = 'test')

@php echo $index; @endphp
Capture d’écran 2023-08-03 à 16 04 50

In the ouput file the PHP end tag is missing:

<?php($index = 'test')

@php echo $index; ?>
<?php /**PATH /Users/marcbelletre/Sites/laravel/resources/views/welcome.blade.php ENDPATH**/ ?

I also tried with older versions of Laravel (8 & 9) and got the same issue.

Steps To Reproduce

  1. Create a blade template with a single line @php directive and a block of raw PHP using the @php ... @endphp syntax
  2. Add a route that renders the blade template
  3. Open the URL in the browser
@marcbelletre marcbelletre changed the title Single @php Blade directive does not work Single @php Blade directive does not work correctly when followed by a @endphp tag Aug 3, 2023
@crynobone
Copy link
Member

For such usage it would be best to just use:

@php
$counter = 1;

echo $counter;
@endphp

or even

@php($counter = 1)

{{ $counter }}

@crynobone
Copy link
Member

A similar PR has been rejected before: #45390 (comment)

@marcbelletre
Copy link
Author

Ok but I think there should be a warning about this in the docs if this is a known issue. I struggled for a while because I couldn't see what was wrong in my code before noticing it was coming from a bug in Laravel itself.

I encountered this with roots/sage which is a package that makes a bridge between Wordpress and Laravel.

The default templates are using the following syntax:

@extends('layouts.app')

@section('content')
  @while(have_posts()) @php(the_post())
    ...
  @endwhile
@endsection

Because of this bug it is impossible to use any raw PHP tags inside the loop which is quite a problem. The only workaround is to replace the default @php(the_post()) call to @php the_post() @endphp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants