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

@pushonce doesn't seem to work as Laravel's blade. #192

Open
n1njatalon opened this issue Jul 10, 2024 · 4 comments
Open

@pushonce doesn't seem to work as Laravel's blade. #192

n1njatalon opened this issue Jul 10, 2024 · 4 comments

Comments

@n1njatalon
Copy link

When using @pushonce for some styles in a component, it seems that it is duplicating the styles. Looking at the docs and code it seems that the @pushonce will only work if used in the same file multiple times. However Laravel's blade uses it per component used as specified in their docs:

The @once directive allows you to define a portion of the template that will only be evaluated once per rendering cycle. This may be useful for pushing a given piece of JavaScript into the page's header using stacks. For example, if you are rendering a given component within a loop, you may wish to only push the JavaScript to the header the first time the component is rendered.

Here is copy of my component:

<div class="{{$additionalclasses ?? ""}}">
    @if($slot)
        <div class="d-flex column-gap-4">
            <p class="flex-grow-2 fw-bold mt-1">{!! $slot !!}</p>
        </div>
    @endif
    <div class="btn-group {{$slot ? "me-5" : ""}}" role="group">
        <input type="radio" class="btn-check" name="{{$name}}"
               id="{{$name}}Yes" autocomplete="off" value="1">
        <label class="btn btn-primary border rounded-start fw-bold" for="{{$name}}Yes">Yes</label>

        <input type="radio" class="btn-check" name="{{$name}}"
               id="{{$name}}No" autocomplete="off" value="0">
        <label class="btn btn-primary border rounded-end fw-bold" for="{{$name}}No">No</label>
    </div>
</div>

@pushonce('styles')
    <style>
        .btn-group>label {
            min-width: 50px;
        }
    </style>
@endpushonce
@MichelJonkman
Copy link

I'm experiencing the same issue, this is normally a very useful feature so it'd be very nice to see this brought inline with the way Laravel works!

@MichelJonkman
Copy link

MichelJonkman commented Oct 8, 2024

Changing the compilePushOnce() method to the below code will fix it:

public function compilePushOnce($expression): string
{
    $key = '';

    if ($this->currentView) {
        $key = '_' . $this->currentView;
    }

    $key = '__pushonce__' . \trim(\substr($expression, 2, -2)) . $key;
    return $this->phpTag . "if(!isset(\$GLOBALS['$key'])): \$GLOBALS['$key']=1;  \$this->startPush$expression; ?>";
}

If @jorgecc @jorgecc-business-account could change this I would really appreciate it! I'd make a pull request but that's a little much work with how small of a change this is.

@jorgecc
Copy link
Member

jorgecc commented Oct 11, 2024

I added another change to the code. Now it is possible to display a stack() before it is called(), i.e.

Now this code is valid:

@stack("example1")
@push("example1")
hello
@endpush

and this code is also valid

@push("example1")
hello
@endpush
@stack("example1")

Example:

@MichelJonkman
Copy link

MichelJonkman commented Oct 14, 2024

I added another change to the code. Now it is possible to display a stack() before it is called(), i.e.

Now this code is valid:

@stack("example1")
@push("example1")
hello
@endpush

and this code is also valid

@push("example1")
hello
@endpush
@stack("example1")

Example:

Is the issue described now fixed as well? I don't see any changes in the code regarding @pushonce

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

No branches or pull requests

3 participants