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 .'\'); ?>';
});
10 Likes

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>
<ul>
  @forelse( $fruits as $fruit )
  <li>{{ $fruit }}</li>
  @empty
  <li class="alert alert-danger">No Fruits!</li>
  @endforelse
</ul>

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

Controller:

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;
    }
}

View:

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

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

2 Likes

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

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

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.

5 Likes

Well that did it, thank you!

1 Like

Man, this is so nice. Kudos @withjacoby!

3 Likes

@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:

<?php

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)
  empty

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

./resources/views/single.base.php

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.

2 Likes