Passing data between filters and templates not working

Hello everybody :smiley:

i’m having some issue while trying to use Sage 9 for the first time.
Specifically, it seems that no matter what I try there’s no way to get any values from the $data array inside the template.

I’ve stepped trough the code with xdebug, and I can confirm the filter gets properly registered with the right template, but once the debugging process reaches the template there’s no trace of $data in the stack. Both $data and the contents of the array are undefined.

here’s the call:

<?php

namespace App;

add_filter('sage/template/template-bottega/data', function () {
$data = [
    'people' => [
        'nome' => 'Nome 1',
        'descrizione' => 'Descrizione 1',
        'foto' => 'https://www.bottega.local/wp-content/example.png',
        'ruolo' => 'Cuoco'
    ]
];
return $data;
});

in views/template-bottega.blade.php

{{--
  Template Name: Bottega
--}}

@extends('layouts.app')

@section('header')
  @component('partials.headers.header-page')
  @endcomponent
@endsection
<h1>{{ $people }}</h1>
@section('content')
  @while(have_posts()) @php the_post() @endphp
    @include('partials.content-page')
  @endwhile
@endsection

Other than a “Undefined Variable Error” All I get is the following exception:
Illuminate\Contracts\Container\BindingResolutionException: Target [Illuminate\Contracts\Container\Container] is not instantiable while building [Illuminate\Events\Dispatcher].

Any help would be appreciated.

Cheers,
Salvo

You may want to see Sage 9 - Passing ACF data via Blade not working for an explanation of what’s going on.

This is supposedly fixed in Sage 9.0.1, but there are a few things that will need to be updated. You can try manually applying the changes at https://github.com/roots/sage/pull/2025 by stepping through each incremental change. Here’s the breakdown:

in /app/controllers/, rename the files app.php -> App.php and front-page.php -> FrontPage.php.

In these two renamed files, change this line:

namespace App;

to

namespace App\Controllers;

Finally, in the file composer.json at the root of the theme, replace this line:

"soberwp/controller": "~9.0.0-beta.4"

with

"soberwp/controller": "2.0.1"

You can then run composer require soberwp/controller:2.0.1 from the command line to force the update.

Give this a try and see if it resolves your problem! This has not yet been patched into the current release of Sage 9 as far as I can tell.

1 Like