Skip to main content
Jack Sleight .DEV
Statamic

Using Statamic globals in other application views

Statamic globals are automatically added to all views rendered by Statamic, whether you’re using Antlers or Blade. However, if you have views that are rendered outside of Statamic directly through Laravel’s view renderer the globals will not automatically be available.

To make globals available in all application views you can add the following function to a service provider's boot method. This will fetch all Statamic globals and add them, plus the current site, to Laravel’s view renderer:

# app/Providers/AppServiceProvider.php
use Illuminate\Support\Facades\View;
use Statamic\Facades\GlobalSet;
use Statamic\Facades\Site;
use Statamic\Statamic;
 
Statamic::booted(function () {
View::share('site', Site::current());
$sets = GlobalSet::all();
foreach ($sets as $set) {
View::share($set->handle(), $set->inCurrentSite());
}
});
# app/Providers/AppServiceProvider.php
use Illuminate\Support\Facades\View;
use Statamic\Facades\GlobalSet;
use Statamic\Facades\Site;
use Statamic\Statamic;
 
Statamic::booted(function () {
View::share('site', Site::current());
$sets = GlobalSet::all();
foreach ($sets as $set) {
View::share($set->handle(), $set->inCurrentSite());
}
});

You can then access these variables as normal in all your application view templates:

{{ $site->locale }}
{{ $footer->small_print }}
{{ $site->locale }}
{{ $footer->small_print }}
8th May 2022