[Blade] Check for empty @stack

A @stack should be outputted together with extra markup.
How can I only output the extra markup if the @stack is not empty?

Example:

<section class="map acf-map">
    @stack('markers')
</section>

How to avoid the element markup if the ‘markers’ stack is empty?

I would wrap the @stack in an @if statement that checked whatever relationship or repeater field generates the markers.

In this case it is a real stack to which something is @pushed.
I can’t use the stack name in @if though.

   @if($markers)
      <section class="map acf-map">
        @stack('markers')
      </section>
    @endif

Notice: Undefined variable: markers

I mean use whatever logic you’re using to generate the stack. So if it’s an ACF relationship field, use @if(get_field('relationship_field_name')) or whatever.

Try this:

/**
 * Determine if the stack has content.
 *
 * @param  string $stack
 * @return boolean
 */
function stack_has_content(string $stack) : bool
{
    return (boolean) trim(sage('blade')->yieldPushContent($stack));
}
1 Like