Skip to content

[12.x] Add @​context Blade directive #56146

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

Open
wants to merge 1 commit into
base: 12.x
Choose a base branch
from

Conversation

martinbean
Copy link
Contributor

@martinbean martinbean commented Jun 26, 2025

Adds a new @context Blade directive, that operates similar to the existing @session directive.


Context Directives

The @context directive may be used to determine if a context value exists. If the context value exists, the template contents within the @context and @endcontext directives will be evaluated. Within the @context directive’s contents, you may echo the $value variable to display the context value:

@context('foo')
    <div>{{ $value }}</div>
@endcontext

This makes it easy to check if a context value is set and if so, to obtain its value, in Blade templates. My particular use case:

@context('canonical')
    <link href="{{ $value }}" rel="canonical">
@endcontext

@martinbean martinbean changed the title [12.x] Add @​context Blade directive [12.x] Add @​context Blade directive Jun 26, 2025
@devajmeireles
Copy link
Contributor

This is good and useful. The only problem that I see with it is the fact that the context has two data: normal and hidden, so it might be better to add an argument @context('foo', hidden: true) - to determine which context you want to interact with, or add two separate directives: @context and @contextHidden

@martinbean
Copy link
Contributor Author

@devajmeireles It just delegates to the context helper, which doesn’t make a distinction between hidden and non-hidden values when retrieving them:

if (! function_exists('context')) {
/**
* Get / set the specified context value.
*
* @param array|string|null $key
* @param mixed $default
* @return ($key is string ? mixed : \Illuminate\Log\Context\Repository)
*/
function context($key = null, $default = null)
{
$context = app(ContextRepository::class);
return match (true) {
is_null($key) => $context,
is_array($key) => $context->add($key),
default => $context->get($key, $default),
};
}
}

@devajmeireles
Copy link
Contributor

@devajmeireles It just delegates to the context helper, which doesn’t make a distinction between hidden and non-hidden values when retrieving them:

if (! function_exists('context')) {
/**
* Get / set the specified context value.
*
* @param array|string|null $key
* @param mixed $default
* @return ($key is string ? mixed : \Illuminate\Log\Context\Repository)
*/
function context($key = null, $default = null)
{
$context = app(ContextRepository::class);
return match (true) {
is_null($key) => $context,
is_array($key) => $context->add($key),
default => $context->get($key, $default),
};
}
}

I see, maybe we can adapt the helper before your PR to enable interaction with hidden values.

@devajmeireles
Copy link
Contributor

@martinbean I just noticed we can interact with the hidden data even if we do not have the hidden argument as part of the helper. All we need to do is interact with the helper without specifying the key:

context()->has('foo');
context()->hasHidden('bar');

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