Roots Discourse

Best Practice / Resources for Blade

Use a static function so you can run a function within each layout.

Thanks for the response.

I noticed that same variable values are being displayed on different flexible content rows; despite the values in the backed of WP.

How would you go about creating a unique variables inside of the flexible content loop with the controller?

Hey did you end up using this way of getting the name of the current blade template? Because I tried to implement it the exact same way (also using barba.js in my project), but I can’t get it to work. I get the error message “undefined variable: current_template in …/wp-content/uploads/cache/350a511d5b8c77023912d976ba1f2ffcdbf847ee.php”.

I added the filter in “my-theme/app/filters.php”. Maybe I’m missing something?! A pointer would be much appreciated!

sage version: 9.0.1

Are you using Controller?

You should be able to add this to App.php if you’re using Controller.

Hey, I already updated to “soberwp/controller”: “2.0.1” and followed instructions from other threads for the changed file names etc. So I’m able to get data of simple strings with the controller into my views, but not really sure how to use the filter for the name of the current blade template with the controller. That’s why I tried to use the filter method in “app/filters.php”.

That’s a good usecase, got some work to finish up, but I’ll take a look at a nicer way of doing this via the App Controller this evening.

1 Like

@williharder this is quite an easy fix for Controller, so I’m going to push a new version over the weekend.

It will solve this and

So in summary add_filter should work again as expected.

Update: Pushed to dev-master for testing.

@williharder to take this further, the next release (which I’ll tag in the next few hours), is going to have a __before (and __after) method which will allow you to interact with Controller params. One of them, is

protected $template; 

So in your specific Controller, you could use this lifecycle event

public function __before()

I hadn’t needed to learn too much about Blade to stay fairly DRY until now. So I watched the Blade section in this tutorial, and 30-60 minutes of taking notes and watching was so worth it. Really cleared up a couple confusions I had w/ Blade.

I’ve done this guy’s tuts before (on Udemy for Vue.js), and he is one of my favorites.

Pluralsite isn’t free, but they do have free trials (no affiliate link or anything sneaky):

1 Like

Thanks, I was also looking to learn better practices, will definitely check out.

I also found some blade tutorials on Laracast. Components and slots is a cool feature that is probably overlooked but most:

1 Like

I recently started using S3-Uploads and I run into this issue. Thanks a lot for mentioning this solution, it worked for me as well. Have you found a better way to counter this issue since you last posted your solution? Thanks!

Thought I’d chime in with my directives package:

Right now there is no documentation and I’m a little pressed on time, but the source code is a quick read to see what is readily available.

For documentation, I will probably just do a MD table or something in the if anyone is feeling froggy. :wink:



Awesome code, I’m using it right now on one of the sites I’m working on. Have a Quick q.

Did you end up having to use WP_Query for when you wanted pagination? I see get_posts as not working when you want to use pagination.

get_posts and WP_Query behave differently.

The first will simply grab an array of posts for you to use and manipulate.

Whereas WP_Query is more for building out a page to list posts whilst leveraging the rest of the WP API.

In other words: get_posts has no relation to WP_Query pagination.

Read more:

The only thing that worked for me was:

`add_action('the_post', function() {
    global $template;
    sage('blade')->share('current_template', basename( $template ,'.blade.php' ));


Hi, we are really trying to understand how to use layouts with @extends / @yield but we ran into a problem during our dev and we can’t figure why it’s not working the ways we want to… Here the stackoverflow :

This forum was full of good ideas and inspire a lots of this stuff maybe someone here can help us understand what we did wrong or why this is happenning!