Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.x] Add blade @cascade directive #10674

Merged
merged 6 commits into from
Aug 21, 2024

Conversation

jacksleight
Copy link
Contributor

@jacksleight jacksleight commented Aug 21, 2024

When using blade the cascade data is only available in the entry's main template, and the layout if you're using @extends. This can leave situations where you don't have quick access to the cascade data (without workarounds), for example:

  1. When inside blade components, particularly when using them for the layout
  2. When in standard views loaded by non-Statamic routes/controllers

This PR adds a new blade directive called @cascade which will add cascade data to the current scope.

It works in exactly the same way as the @props directive used in blade components, with the ability to require certain values and/or provide default fallback values:

{{-- Fetch my_global and site, will throw an exception if either are missing from the cascade --}}
@cascade([
    'my_global',
    'site',
])

{{ $site->locale }}
{{-- Fetch live_preview and page, use fallback if they're missing from the cascade --}}
@cascade([
    'live_preview' => false,
    'page' => null,
])

{{ $page?->title }}

Specifying only the data you want is probably the best way to go in most situations, especially in components where the cascade variables could overwrite prop variables. However it is also possible to add the entire cascade if needed:

{{-- Fetch everything from the cascade --}}
@cascade

@jasonvarga
Copy link
Member

Great idea

@jasonvarga jasonvarga merged commit 4f38029 into statamic:5.x Aug 21, 2024
18 checks passed
@jacksleight jacksleight deleted the feature/cascade-directive branch August 22, 2024 07:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants