Best Practice / Resources for Blade

Have you also ensured that your composer minimum-stability is set to beta?

Any ideas how to implement the @optional directive from your Laracast link?

It references the yieldContent method.

how would you reference the post1 function inside of the post2 function?

How would you reference one function inside of another function using this syntax?

I’ve been enjoying my @shortcode directive for blade, as simple as it is…

 * Create @shortcode() Blade directive
sage('blade')->compiler()->directive('shortcode', function ($shortcode) {
    return '<?= do_shortcode(\''. $shortcode .'\'); ?>';

Hey, sorry, I’ve been wrapped up with work stuff. How about -

// fruit stand entrepreneur
function name($name)
    return $name . '\'s Fruit World.';
// fruits for sale
function fruits()
    $fruits = ['apple', 'orange', 'pineapple'];
    return $fruits;
// use on a page called Fruit Stand (ie. body class  fruit-stand)
add_filter('sage/template/fruit-stand/data', function () {

    $data['name']   = name('Bob'); // name function - string
    $data['fruits'] = fruits(); // fruits function - array
    return $data;  // return data

Template -

<h2>{{ $name or 'Name not defined' }}</h2>

<h3>Fruits for Sale</h3>
  @forelse( $fruits as $fruit )
  <li>{{ $fruit }}</li>
  <li class="alert alert-danger">No Fruits!</li>

What are you trying and what sorts of problems are you running into?

1 Like

Curious, how would I setup a custom loop utilizing the controller? My use case is that for single posts I also want to fetch all other posts to show in a list on the same page.

This is what I’m trying


use WP_Query;
use Sober\Controller\Controller;

class Single extends Controller {
     * Return all People posts
     * @return array
    public function people() {
      $people = new WP_Query( [ 'post_type' => 'people' ] );
      return $people;


@while($people->have_posts()) @php(the_post())

Which results in
Xdebug: Fatal error: Uncaught Symfony\Component\Debug\Exception\FatalThrowableError: Call to a member function have_posts() on null in


@intelligence been able to get it working on my side, try the following

@while($people->have_posts()) @php($people->the_post())
    {{ the_title() }}

If it’s still not working, you can use @debug or @debug(‘dump’) within the Blade directive to see if it’s returning $people correctly.


Well that did it, thank you!

1 Like

Man, this is so nice. Kudos @withjacoby!


@withjacoby Hi! I’m struggling with getting controller to work, wondering if you could help me out.

I’m running

  • Sage 9.0.0-beta2
  • Controller 9.0.0-beta2.1 installed and activated as a MU plugin

In ./src/controllers/single.php I have the following:


namespace App;

use Sober\Controller\Controller;

class Single extends Controller
    public function hello()
        return "hello";

Calling {{$hello}} in ./resources/views/single.blade.php returns:

Undefined variable: hello

I ran debugging and get the following:

// @debug('hierarchy')

Hierarchy Debugger:
- /src/controllers/base.php
- /src/controllers/singular.php
- /src/controllers/single.php
- /src/controllers/single-episode.php
// @debug('controller')

Controller Debugger:

// @debug('dump')

. /current/web/app/mu-plugins/controller/src/Module/Debugger.php:55:
array (size=0)

@anon21078981 just want to make sure before setting up my side, is this a typo?


If not, try rename to single.php and let me know?

Also, just noticed, ./resources/view/ is for dev-master. For 9.0.0-beta.2, it should be ./templates/single.blade.php

@withjacoby Thanks for the quick response. Indeed, I meant to say ./resources/views/single.blade.php.

Renaming it to single.php breaks Sage.

That’s strange, package.json reads "version": "9.0.0-beta.2".

Perhaps I’ll try reinstalling Sage.

Sorry, yes ./resources/views/single.blade.php is correct.

I think try reinstalling and let me know if that helps, it’s likely not finding the Controller files if the path filter is incorrect, which is different between dev-master and 9.0.0-beta.2 so that may be the problem.

I just re-ran composer create-project roots/sage your-theme-name dev-master and package.json still reads "version": "9.0.0-beta.2".

My views are still in ./resources/views/. Do you know if this is normal?

That doesn’t sound correct.

But you do have the dev-master version, so you could try the dev-master version of Controller which will work with your version.

I reinstalled dev-master versions of both Sage and Controller and I’m still having the same issue.

Do you have any tips on how I should debug?

@anon21078981 any chance you can upload your theme folder to github and I’ll take a look?

You can DM me your email so we don’t flood this thread.

Hey, I was getting undefined variables in my Base controller, too. I’m using dev-master for both Controller and Sage.

Turns out base.blade.php was renamed to app.blade.php recently. Renaming my controller fixed the issue.

1 Like

That’s correct. dev-master is on par with Sage dev-master, so using resources/controllers/App.php is the correct usage.

I’ve updated the docs to reflect this.