Confusing documentation on Composers

Hello.
I’m into learning “sage” and especially reading about Composers in the template context.
It’s a somewhat obscure topic to me, but I’m holding fast.
On my path to knowledge I stumbled upon this blog post:

And it sent me back into useless error loops - it is particularly misleading and doesn’t help learners at all as it’s literally giving non - working code samples.

Example:

// Composers/Header.php
namespace App\Composers;

What?
I’ve read elsewhere ( https://docs.roots.io/sage/10.x/composers/ )
that the namespace was namespace App\View\Composers;

use Roots\Acorn\View\Composer;
class Header extends Composer
{
    protected static $views = [
        'partials.header',
    ];

    public function with($data, $view)
    {
        return [
            'img_url' => get_attachment_image_url(get_post_thumbnail_id()),
            'class' => 'hero',
            'title' => $post->post_title,
        ];
    }
}

Some of the confusing points for this example alone,
1- get_attachment_image_url isn’t a wordpress function
2- $post is undefined in this context.
3- public function with($data, $view) or public function with() ?
( Method 'App\View\Composers\WhatEver::with()' is not compatible with method 'Roots\Acorn\View\Composer::with()'.
4- " Get it from $data"

$data is the context as it exists when your view is @includeed. This means that any variable available to that view is available to your View Composer as well. $data is a keyed array, and the keys are the same as the variable names that can be used in the blade.

Nope, $data is undefined in this context.
$this->data->get('post') is undefined.
How are you exactly supposed to get WP_post object values in that context?
Sure you can use native WP functions but isn’t it a built-in object that I could use?

And so forth - maybe i’m missing something also -
No pun intended but this single piece hold me back in a lot of errors and misunderstanding, and this post is to maybe avoid that pain to other new learners.

1 Like

Sorry for the problems you’re running into. That post is getting close to four years old at this point, and some of it (like the $data stuff) is outdated. The docs are what you should trust, and are currently a much better reference.

That is my point: it’s confusing for a beginner and hardens the learning curve.

What would you recommend using inside a composer to access current WP_post object?
I’m actually relying on native WP functions ( like 'ID' => get_the_ID(), but is there an object like $data we can use, maybe I’m overdoing it?

Getting started with Sage 10 and the View Composers I also went ahead with this guide only to later notice that some of the stuff is outdated…

I’d suggest to add a disclaimer on top of the post stating that some of the infos in the post aren’t accurate any more and the docs should be consulted.

Is that something you would be open for @alwaysblank?

1 Like

We’ve redirected that post to the docs page for now — thank you for bringing this to our attention!

1 Like