Roots Discourse

How to use @isset blade directive with custom loop from cotroller

sage9

#1

Hello
…sorry, this is maybe a basic php/blade question, but I can’t find a solution around. I’m tryng to figure out how to use the @isset (or an alternative) to display a data value from a controller custom loop.

Say this is my custom loop (in FrontPage.php controller):

public static function latestItemsLoop()
{
    $args = array(
        'post_type' => 'items',
        'posts_per_page' => 6,
    );
    $index_items = get_posts($args);

    return array_map(function ($post) {
        return [
            'itemTitle' => apply_filters('get_the_title', $post->post_title),
            [...etc...]
        ];
    }, $index_items );
}

Now in the view.blade file Id’ like to use something like this:

@foreach(FrontPage::latestItemsLoop() as $indexItem)
	@isset( $indexItem['itemTitle'] )
		<h4>{!! $indexItem['itemTitle'] !!}</h4>
	@endisset
@endforeach

…or maybe an “if” condition like (semantically) “if !isnot set” or “if !isnot empty”

I’ve found the @isset in Laravel Blade documentation, but now I don’t know how to deal with Sage 9 views {{!! variable !!}} data


#2

Not sure if I am following correctly, but try using @unless(empty($indexItem['itemTitle'])) instead.


#3

Hello @codepuncher
…that’s it - it works! :slight_smile:

Thank you…

(Now I’m facing other problems about Sage 9/Blade and WPML plugin for multilanguage …that I think deserve an dedicated forum thread…)


#4

No problem!

An alternative is also:

@empty($var)
    No results 
@else
    {{ $var }}
@endempty

Check out the Blade docs for more directives.