Roots Discourse

Frustrating experience with Blade

blade

#1

Hi guys,

I’ve written a couple of websites using Sage and, while it’s a great tool, the bizarre problems I’ve been having with blade is getting me down.

Here’s an example.

I have written the following logic:

// snip
    <section class="posts">
      @while (have_posts()) @php(the_post())
        @php
          global $post;
          echo $post->menu_order;
        @endphp
        @include('partials.content-'.get_post_type(),
        ['image' => $controller::getImage()])
      @endwhile
    </section>

I iterated to this working point, and a few iterations ago it had errors - fair enough, but now that the code is actually fine, blade hasn’t caught up and insists there’s an error.

This is what I see, using the WHOOPS debugging tool:

This shows that blade is not catching up with the changes.

What do I have to do to get it to behave?


#2

You need to add closing tags (i.e @endphp) for all @php directives now. I forget which version, but it was a change in Laravel.


#3

To be explicit, it’s this line that needs to be changed:

<section class="posts">
  @while (have_posts()) @php(the_post()) <--- right here!!
  ...

Changing it to this should fix your issue:

// snip
<section class="posts">
  @while (have_posts()) @php the_post() @endphp
  ...

#4

Thanks guys - it all makes sense now!

The inline bit @php( ... ) seemed to work fine before I added the @php ... @endphp bit underneath. Maybe it was loading from the blade cache, from a previous version of blade that knew how to parse it.

I was frustrated because I’d had similar things happen before where I “knew” the code was right but blade didn’t want to update its position on the matter, or would complain about an issue and then load fine on the next page reload - maybe a race condition in the webpack watcher.

Anyway, THANK YOU! :smile: