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

Added @returnifempty directive. #66

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

yaquawa
Copy link

@yaquawa yaquawa commented Jul 22, 2020

Thanks for the nice package!

I added a new @returnifempty directive.
I've been using this very frequently. Hope this can be merged.

@gizburdt
Copy link
Member

Hi, thanks for the PR. There is already a directive called @empty, so I think a better name would be notEmpty. Could you change it? :)

@yaquawa
Copy link
Author

yaquawa commented Jul 23, 2020

Hi @gizburdt, if we just call it notEmpty it seems like a normal conditional directive.
But this one will return if it is empty. Directive should be self described right?

@gizburdt
Copy link
Member

I talked about this with @ovanschie, but we're wondering when you would use this? It feels like logic needs to be placed inside PHP/Controller/Model and not inside Blade. Can you paste a snippet where you use this directive?

@yaquawa
Copy link
Author

yaquawa commented Jul 23, 2020

@gizburdt

For example, suppose you have a view component components/posts.blade

@props([
    'posts'
])

@returnIfEmpty($posts)

<section class="container">
    <h1>My Posts</h1>
    
    <div class="posts">
        @foreach($posts as $post)
            <div class="posts__item">{{ $post->title }}</div>
        @endforeach
    </div>
</section>

Then you can use it like this

<div>Foo</div>

{{-- if the variable $posts is empty, it got nothing to render. --}}
<x-posts :posts="$posts"/>

<div>Bar</div>

without the @returnIfEmpty, it could be written as this, which is too verbose for me.

<div>Foo</div>

@if(count($posts))
    <x-posts :posts="$posts"/>
@endif

<div>Bar</div>

@yaquawa
Copy link
Author

yaquawa commented Sep 14, 2020

Hi @gizburdt , would you please tell me if you are going to merge this PR in the future or not? Thanks.

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