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 ( Sage 10.x: Composers | Roots Documentation )
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@include
ed. 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.