Timber v2 Issue

508 Views Asked by At

We are using Timber v1 but are now required to migrate to v2 since Dreamhost will soon require that all sites are upgraded to PHP8.*]

Problem is, the new version is now working following the instructions. Here's the current fatal error:

Fatal error: Uncaught Error: Call to protected method Timber\Timber::init() from global scope in /home/wp_q5bibz/incrementumdigitalcom.stage.site/wp-content/themes/id/functions.php:26

Followed the instruction here: https://timber.github.io/docs/v2/installation/

2

There are 2 best solutions below

1
thaikolja On

As of today, the latest version is 2.0.0-rc.1. If you install Timber via Composer, make sure to install this specific version via composer require timber/timber:2.0.0-rc.1 or change it in your composer.json.

Alternatively, download it from GitHub.

For more context, composer is currently automatically pulling timber/timber 1.22.1 as of early Oct. If you are new to composer, you can check all package versions with composer.phar show. When updating to timber/timber:2.0.0-rc.1, you might also need to update dependencies. Composer will let you know what dependencies and you can update them with composer update package/package

0
Charles Leverington On

Not sure which fix got me there, but there original poster is correct that the Starter theme "as-is" (2.0.0, Dec. 7, 2023) is non-functional on default install.

In addition, there's some documentation in the Timber 2.x handbook (as of this post in Feb 2024), which seems non-functional as well.


  1. Use the latest version of Timber.
  • Make the following change and run composer install.
  • (If you've already run composer install either delete the generated composer.lock file or run composer update timber/timber --with-dependencies instead of composer install.)
"require": {
        "timber/timber": "^2.0"
    },
  1. Comment out these two lines from The StarterSite class-file. (The tooling within them matches the OLD 2.x setup, but not the NEW setup.)
add_filter( 'timber/twig', array( $this, 'add_to_twig' ) );
add_filter( 'timber/twig/environment/options', [ $this, 'update_twig_environment_options' ] );

(I got my custom Twig function to work using the old-school method, but even the official documentation behind enabling auto-escaping seems non-functional altogether.)

  1. I added PSR-4 autoloading, which seemed to help:
  • Add namespace StarterSite; to the top of src/Startersite.php
  • Comment out and/or remove the require_once tag for the StarterSite.php, since Composer auto-loading will handle it.
  • Add the following to to your theme-level composer.json file:
"autoload": {
   "psr-4": {
       "StarterSite\\" : "src"
   }
}
  • Run composer install
  1. I did enough that I'm not sure THIS step made a difference.
  • I didn't trust the "Timber" autoloading, because it didn't seem to work?
  • Instead I started adding use Timber\Timber; and/or use Timber{ Site, Timber }; as needed on functions.php, my template files, and the StaterSite.php class file.