ekandreas / bladerunner

WordPress plugin for Laravel Blade templating DEPRECATED
http://bladerunner.elseif.se
12 stars 1 forks source link

Putting the loop in a template #76

Closed vpillinger closed 3 years ago

vpillinger commented 6 years ago

So I had attempted to put the WP loop into my main template, as essentially every page in wordpress requires it to function. However, this did not work.

I was forced to include the while loop individually into each page.

I am not fully understanding the potential cause of this because $wp_query is supposed to be a global variable. Here is a code example

In main layout

@if (have_posts())
  @while(have_posts()) @php the_post() @endphp
    @yield('content')
   @endwhile
@else
  @yield('no-content')
@endif

In individual pages

@section('content')
{!!the_content()!!}
@endsection
ekandreas commented 6 years ago

It's not supposed to work in that way. Please consider something like this:

<?php
// index.php
echo view('views.index', [
    'posts' => have_posts() ? get_posts() : null,
]);
<?php
// views/index.blade.php
@foreach($posts as $post)
    @php(setup_postdata($post)
    ....
@endforeach

With this example I would encurage you to use the template engine as supposed to. Send data to the template. Don't let the template generate it's own data.

I have not tested the code above but I hope you get the principe. Send data to the template.

vpillinger commented 6 years ago

Ah. It seems like my ignorance of the setup_postdata($post) method was what was preventing me from doing something like this in the first-place.

However, this still does not solve the challenge of having to put the for-loop in every layout. In this case, my "content" section of my layout will always contain the following boilerplate.

<?php
@extends ('views.layouts.main')
// section implementing main layout
@section('content')
@foreach($posts as $post)
    @php(setup_postdata($post)
    ....
@endforeach
@endsection

Additionally, I found it useful to setup my main layout to have a section for "no-content" so it would be easy to set a fallback section on large templates without having to add a large if-statement surrounding lots of code. The above would not allow this.

Perhaps there is a helper function or similar that can be created that will allow people to setup a section in their layout that is assumed to be in "the loop". Meaning all the variables are set correctly so functions like the_content() work correctly in that section.

One of the big draws to blade for me is being able to put all of the boilerplate in the layout so the individual pages only need to implement what changes from page to page.

vpillinger commented 6 years ago

I had an idea on this. We should just add a custom blade directive that allows one to easily loop through a list of a posts.

Something like

@loopposts ($posts)
 {--- stuff ---}
@endloopposts

OR for wp_query

@loopposts ($wp_query)
 {{-- stuff --}}
@endloopposts

The loopposts directive can handle the setting up of a proper loop.

The big advantage of this is that it can also handle things like fixing the pagination for custom queries automatically: https://wordpress.stackexchange.com/questions/120407/how-to-fix-pagination-for-custom-loops

Does this seem like something that would be useful? It would allow us to make use of wordpress built-in functionality, while also easily sending data to the templates without worrying about global query and post objects. If implemented right, it should also make it easy to nest custom post queries.

ekandreas commented 3 years ago

Hi! This is a deprecated package. Please use https://github.com/EFTEC/BladeOne instead!