Best Practice / Resources for Blade

blade
sage9

#81

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


#82

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

It references the yieldContent method.


#83

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


#84

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


#85

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

#86

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?


#88

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


#89

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


#90

Well that did it, thank you!


#91

Man, this is so nice. Kudos @withjacoby!


#92

@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

#94

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


#95

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


#96

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.


#97

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?


#98

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.


#99

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?


#100

@seb2point0 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.


#101

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.


#102

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.