Coming back to this; I have merged all of the changes in the pull request. I cloned a new copy of Sage 9.0.1, and did the following:
- Rename
app/controllers/app.php
and front-page.php
to App.php
and FrontPage.php
- Change the namespace in both of the above files to be
namespace App\Controllers;
- Update
composer.json
to have "soberwp/controller": "2.0.1"
and run composer require soberwp/controller:2.0.1
to update.
However, after making these changes, there is still an error about missing data when creating a filter and attempting to call the data in the template.
This is modeled after the guide at Sage 9.x: Blade Templates | Roots Documentation and the eBook.
In app/filters.php
:
/**
* Add data references for page template.
*/
add_filter('sage/template/page/data', function (array $data) {
$data['some_data'] = "This is my data";
return $data;
});
In resources/views/page.blade.php
:
@extends('layouts.app')
@section('content')
@while(have_posts()) @php the_post() @endphp
@include('partials.page-header')
@include('partials.content-page')
<p>{{ $some_data }}</p>
@endwhile
@endsection
On the front end, I created a page titled “about-us” and it throws an error stating that the variable some_data
is undefined. As noted by @alwaysblank, we can see what is going on by adding some var_dump()
calls in filters.php
in the add_filter
call:
/**
* Render page using Blade
*/
add_filter('template_include', function ($template) {
var_dump($template); // Verify the Template being used
$data = collect(get_body_class())->reduce(function ($data, $class) use ($template) {
var_dump($class); // Verify the classes being computed
return apply_filters("sage/template/{$class}/data", $data, $template);
}, []);
var_dump($data); // Verify the data that has been compiled
if ($template) {
echo template($template, $data);
return get_stylesheet_directory().'/index.php';
}
return $template;
}, PHP_INT_MAX);
Our output shows the template is correct:
/var/www/html/wp-content/themes/wp-sage-theme/app/filters.php:68:string '/var/www/html/wp-content/themes/wp-sage-theme/resources/views/page.blade.php' (length=85)
Here is the list of classes being applied:
/var/www/html/wp-content/themes/wp-sage-theme/app/filters.php:70:string 'page-template-default' (length=21)
/var/www/html/wp-content/themes/wp-sage-theme/app/filters.php:70:string 'page' (length=4)
/var/www/html/wp-content/themes/wp-sage-theme/app/filters.php:70:string 'page-id-14' (length=10)
/var/www/html/wp-content/themes/wp-sage-theme/app/filters.php:70:string 'logged-in' (length=9)
/var/www/html/wp-content/themes/wp-sage-theme/app/filters.php:70:string 'admin-bar' (length=9)
/var/www/html/wp-content/themes/wp-sage-theme/app/filters.php:70:string 'no-customize-support' (length=20)
/var/www/html/wp-content/themes/wp-sage-theme/app/filters.php:70:string 'about-us' (length=8)
/var/www/html/wp-content/themes/wp-sage-theme/app/filters.php:70:string 'app-data' (length=8)
/var/www/html/wp-content/themes/wp-sage-theme/app/filters.php:70:string 'index-data' (length=10)
/var/www/html/wp-content/themes/wp-sage-theme/app/filters.php:70:string 'singular-data' (length=13)
/var/www/html/wp-content/themes/wp-sage-theme/app/filters.php:70:string 'page-data' (length=9)
/var/www/html/wp-content/themes/wp-sage-theme/app/filters.php:70:string 'page-14-data' (length=12)
/var/www/html/wp-content/themes/wp-sage-theme/app/filters.php:70:string 'page-about-us-data' (length=18)
And the end resulting data:
/var/www/html/wp-content/themes/wp-sage-theme/app/filters.php:73:
array (size=5)
'post' =>
object(WP_Post)[1483]
public 'ID' => int 14
public 'post_author' => string '1' (length=1)
public 'post_date' => string '2018-06-21 14:04:48' (length=19)
public 'post_date_gmt' => string '2018-06-22 00:04:48' (length=19)
public 'post_content' => string 'Here's where you can learn more about US! And how amazingly AWESOME we are!' (length=77)
public 'post_title' => string 'About Us' (length=8)
public 'post_excerpt' => string '' (length=0)
public 'post_status' => string 'publish' (length=7)
public 'comment_status' => string 'closed' (length=6)
public 'ping_status' => string 'closed' (length=6)
public 'post_password' => string '' (length=0)
public 'post_name' => string 'about-us' (length=8)
public 'to_ping' => string '' (length=0)
public 'pinged' => string '' (length=0)
public 'post_modified' => string '2018-06-21 14:04:48' (length=19)
public 'post_modified_gmt' => string '2018-06-22 00:04:48' (length=19)
public 'post_content_filtered' => string '' (length=0)
public 'post_parent' => int 0
public 'guid' => string 'http://192.168.10.10/?page_id=14' (length=32)
public 'menu_order' => int 0
public 'post_type' => string 'page' (length=4)
public 'post_mime_type' => string '' (length=0)
public 'comment_count' => string '0' (length=1)
public 'filter' => string 'raw' (length=3)
'site_name' => string 'SITENAME' (length=8)
'__debugger' =>
array (size=1)
0 =>
object(stdClass)[1624]
public 'class' => string 'App' (length=3)
public 'tree' => boolean false
public 'methods' =>
array (size=1)
...
public 'data' =>
array (size=2)
...
'__app' =>
array (size=3)
'post' =>
object(WP_Post)[1483]
public 'ID' => int 14
public 'post_author' => string '1' (length=1)
public 'post_date' => string '2018-06-21 14:04:48' (length=19)
public 'post_date_gmt' => string '2018-06-22 00:04:48' (length=19)
public 'post_content' => string 'Here's where you can learn more about US! And how amazingly AWESOME we are!' (length=77)
public 'post_title' => string 'About Us' (length=8)
public 'post_excerpt' => string '' (length=0)
public 'post_status' => string 'publish' (length=7)
public 'comment_status' => string 'closed' (length=6)
public 'ping_status' => string 'closed' (length=6)
public 'post_password' => string '' (length=0)
public 'post_name' => string 'about-us' (length=8)
public 'to_ping' => string '' (length=0)
public 'pinged' => string '' (length=0)
public 'post_modified' => string '2018-06-21 14:04:48' (length=19)
public 'post_modified_gmt' => string '2018-06-22 00:04:48' (length=19)
public 'post_content_filtered' => string '' (length=0)
public 'post_parent' => int 0
public 'guid' => string 'http://192.168.10.10/?page_id=14' (length=32)
public 'menu_order' => int 0
public 'post_type' => string 'page' (length=4)
public 'post_mime_type' => string '' (length=0)
public 'comment_count' => string '0' (length=1)
public 'filter' => string 'raw' (length=3)
'site_name' => string 'SITENAME' (length=8)
'__debugger' =>
array (size=1)
0 =>
object(stdClass)[1624]
...
'__store' =>
array (size=5)
'some_data' => string 'This is my data' (length=15)
'__debugger' =>
array (size=1)
0 =>
object(stdClass)[1624]
...
'post' =>
object(WP_Post)[1483]
public 'ID' => int 14
public 'post_author' => string '1' (length=1)
public 'post_date' => string '2018-06-21 14:04:48' (length=19)
public 'post_date_gmt' => string '2018-06-22 00:04:48' (length=19)
public 'post_content' => string 'Here's where you can learn more about US! And how amazingly AWESOME we are!' (length=77)
public 'post_title' => string 'About Us' (length=8)
public 'post_excerpt' => string '' (length=0)
public 'post_status' => string 'publish' (length=7)
public 'comment_status' => string 'closed' (length=6)
public 'ping_status' => string 'closed' (length=6)
public 'post_password' => string '' (length=0)
public 'post_name' => string 'about-us' (length=8)
public 'to_ping' => string '' (length=0)
public 'pinged' => string '' (length=0)
public 'post_modified' => string '2018-06-21 14:04:48' (length=19)
public 'post_modified_gmt' => string '2018-06-22 00:04:48' (length=19)
public 'post_content_filtered' => string '' (length=0)
public 'post_parent' => int 0
public 'guid' => string 'http://192.168.10.10/?page_id=14' (length=32)
public 'menu_order' => int 0
public 'post_type' => string 'page' (length=4)
public 'post_mime_type' => string '' (length=0)
public 'comment_count' => string '0' (length=1)
public 'filter' => string 'raw' (length=3)
'site_name' => string 'SITENAME' (length=8)
'__app' =>
array (size=3)
'post' =>
object(WP_Post)[1483]
...
'site_name' => string 'SITENAME' (length=8)
'__debugger' =>
array (size=1)
...
Of particular note is this part:
'__store' =>
array (size=5)
'some_data' => string 'This is my data' (length=15)
'__debugger' =>
This results in the following page:
So my question is, at this point, what am I missing? I understand that I can bypass this by just using the controllers directly to add content to templates via functions. My question is why the filters method is not currently functioning after following the steps to get up-to-date, and what I can do to resolve it so that it does.