roots / sage

WordPress starter theme with Laravel Blade components and templates, Tailwind CSS, and a modern development workflow
https://roots.io/sage/
MIT License
12.69k stars 3.06k forks source link

Replace single line PHP statements #3141

Closed marcbelletre closed 8 months ago

marcbelletre commented 1 year ago

Summary

I noticed a bug in Laravel that happens when using a single line @php directive followed by a @endphp tag anywhere in the template file. I created an issue on the laravel/framework repository but I was told it is a know issue for quite a while and there is no plan to fix it.

Because the default template files of Sage are using this syntax, it is impossible to use raw PHP inside the loop.

For example this would throw a compilation exception:

@extends('layouts.app')

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

    @php
    $var = 'something';
    @endphp 

    ...
  @endwhile
@endsection

I would suggest to update the default template files with the multiline syntax so it doesn't happen. The short syntax is easier to read but at least the full one compiles correctly :)

@extends('layouts.app')

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

I also suggested to the Laravel team to add a warning to the docs about this.

Additional context

No response

Log1x commented 8 months ago

https://discourse.roots.io/t/blade-php-endphp-why-is-it-not-working/23713/11 has more context on this.

I understand that not everyone agrees, but this won't be getting changed in Sage's out of the box views unless it were to stop working all together.