Core Concepts
Global Settings
Concept
A Global Settings are a key-value pair that are useful for a small pieces of content that can be reused across your site. An example is a contact phone number that is a button in the navigation as well as a link in the footer.
Global Settings are inherently simple, similar to Notification Bars, so they are only two properties:
- "Key" - A unique name to identify and access the Global Setting.
- "Value" - A singular string that contains information.
Global Settings can be used for a multitude of things, not just limited to displaying exactly what is stored in the "Value." It can hold important metadata to be used across pages, a filename that will render another view component, etc. Think of these in the same way Reusable Blocks are for Content Blocks, you can have a simple Global Setting in the CMS but, within your Blade files, you can do more around the value itself.
Usage
Similar to Menus and Notification Bars,
Global Settings are accessed through their Key property. The only difference here is that every Global Setting
variable is prefixed with global_
when accessing it in Blade. This is to help avoid naming conflicts
with other variables.
An example is a User creates a new Global Setting named "Phone Number." Using the Global Setting
View Composer, that Global Setting
is auto-injected onto a Page and is accessed by its key, i.e. $global_phone_number
.
It is also best practice to wrap any Global Setting rendering in an isset
Blade function. This is
because a User can change the Key of a Global Setting at anytime within the CMS, causing an error when trying to
access a non-existent variable.
1@isset($global_phone_number)2 <!-- Access and render the Global Setting -->3@endisset
Advanced Example
Global Settings can do more than just display their "Value" property exactly. In the below example, a Global Setting was created that has a filename to another View Component that we want to render.
1@isset($global_contact_link)2 <x-dynamic-component :component="$global_contact_link" />3@endisset
1<!-- resources/views/components/global_contact_link -->2<a href="/contact">Contact Us</a>