Skip to content
This repository has been archived by the owner on Jul 16, 2021. It is now read-only.

[Feature] Add a @returnif blade directive #2640

Open
tkaravou opened this issue Jun 15, 2021 · 1 comment
Open

[Feature] Add a @returnif blade directive #2640

tkaravou opened this issue Jun 15, 2021 · 1 comment

Comments

@tkaravou
Copy link

tkaravou commented Jun 15, 2021

In many cases, we have Blade partials that are wrapped up in an if statement to conditionally render them. It would be nice having something like @returnif where we exit the partial right away if the criteria is met

Blade::directive('returnif', function ($expression) {
    return "<?php if($expression) return; ?>";
});

Usage

@returnif (empty($records))

<h3>Record Listing</h3>
<ul>
@foreach($records as $record)
    <li>{{ $record->name}}</li>
@endforeach
</ul>
@tkaravou tkaravou changed the title Add a @returnif as a blade directive Add @returnif as a blade directive Jun 15, 2021
@tkaravou tkaravou changed the title Add @returnif as a blade directive Add a @returnif blade directive Jun 15, 2021
@tkaravou tkaravou changed the title Add a @returnif blade directive [Feature] Add a @returnif blade directive Jun 15, 2021
@ahinkle
Copy link

ahinkle commented Jun 25, 2021

Can you provide why it would be a benefit over the existing conditionals available in the framework? Your suggested implementation could cause unexpected bugs in applications with early returns.

@if (! $records)
    <h3>Record Listing</h3>
    <ul>
    @foreach($records as $record)
        <li>{{ $record->name}}</li>
    @endforeach
    </ul>
@endif

Alternatively:

<h3>Record Listing</h3>
<ul>
@forelse($records as $record)
    <li>{{ $record->name}}</li>
@empty
    <li>No records.</li>
@endforelse
</ul>

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants