1

I have the following hbs snippet:

<div class='photo-viewer-control' local-class='leftbar'>
  {{yield to='leftbar'}}
</div>

I am consuming the leftbar component in:

<Viewer>
  <:leftbar>
    <Infos
      @image={{this.currentImage}}
    />
  </:leftbar>
</Viewer>

I want to render the div only if leftbar has yielded.

I already tried the following:

{{#if (has-block 'leftbar')}}
  <div class='photo-viewer-control' local-class='leftbar'>
    {{yield to='leftbar'}}
  </div>
{{/if}}

I added the has-block but the div is still rendering when there is no leftbar.

adhu
  • 35
  • 6

1 Answers1

0

Your code is very close!

To avoid rendering a :leftbar, omit the block:

<Viewer>
  no left bar will be rendered
</Viewer>

here is a demo of how this works

const Viewer = <template>
  {{yield}}

  {{#if (has-block 'leftbar')}}
    <div>
      {{yield to="leftbar"}}
    </div>
  {{/if}}
</template>;

<template>
  <Viewer>
    <:leftbar>
      leftbar content
    </:leftbar>
  </Viewer>

  <Viewer>
    no leftbar content
  </Viewer>
</template>

(this demo uses the new "gjs" format -- tutorial here, if unfamiliar: https://tutorial.glimdown.com )

NullVoxPopuli
  • 61,906
  • 73
  • 206
  • 352