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

[11.x] Add Blade @var directive #53866

Closed
wants to merge 2 commits into from

Conversation

SanderMuller
Copy link
Contributor

Same idea as #49179 but to add docblocks to improve IDE support like /** var \Namespace\Class $variable */. This is especially useful in Livewire components, Filament views and other Blade files where the IDE can not infer the variable types.

This is only an addition, so shouldn't cause any trouble or disturbance - unless someone has create a custom Blade directive called @var.

A minor addition for convenience when adding extra type information for your IDE in Blade components.

Benefits

  • 1 line instead of 3 🔥
  • Less code to type ⏱️
  • Makes templates look cleaner 🧹

Example (Filament table in this case)

{{-- Before --}}
@php
    /** @var \App\Livewire\Playlist\Index $this */
@endphp

{{-- After --}}
@var(\App\Livewire\Playlist\Index $this)

{{ $this->table }}

Caveats

IDE support won't be there initially for folks, so there will be squigglies.

Credits to @simonhamp for a decent part of this PR description text (based on #49179)

@SanderMuller SanderMuller changed the title Add Blade @var directive [11.x]: Add Blade @var directive Dec 12, 2024
@SanderMuller SanderMuller changed the title [11.x]: Add Blade @var directive [11.x] Add Blade @var directive Dec 12, 2024
@ahinkle
Copy link
Contributor

ahinkle commented Dec 13, 2024

Cool idea but I just don't think it fits well for the framework.

@use is a functional directive that affects the runtime behavior whereas the proposed @var is purely for IDE support.

It could also come off as a bit ambiguous, as new developers might think it's actually declaring/defining variables.

@antonkomarev
Copy link
Contributor

Since IDE doesn't understand proposed syntax, I have another idea.

Here is a common comment block:

{{-- @var \App\Livewire\Playlist\Index $this --}}

Much cleaner, will be omitted from the generated PHP template file since it is just a comment.

But we should add this support to IDEs somehow.

@SanderMuller
Copy link
Contributor Author

Cool idea but I just don't think it fits well for the framework.

@use is a functional directive that affects the runtime behavior whereas the proposed @var is purely for IDE support.

It could also come off as a bit ambiguous, as new developers might think it's actually declaring/defining variables.

You're probably right!

Since IDE doesn't understand proposed syntax, I have another idea.

Here is a common comment block:

{{-- @var \App\Livewire\Playlist\Index $this --}}

Much cleaner, will be omitted from the generated PHP template file since it is just a comment.

But we should add this support to IDEs somehow.

That is indeed a way neater option, which would lie purely in IDE implementation, where this PR's idea would require both Laravel and IDE. The benefit of having it in Laravel is that the IDE will follow to support the Laravel feature (as happened with @use).

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.

3 participants