Dont show section if fields are empty

Hi,

I have this code:

<?php if ($page->extract()->isEmpty()): ?>

    <section>
        <p><?= $page->heading()->text() ?></p>
        <p><?= $page->subheading()->text() ?></p>
    </section>

<?php endif; ?>

If I don’t fill in the field in the panel the paragraph does not show.
But the section still remains.
I want it to be that when fields are not filled in the whole section is not rendering.
How can I make it so that the tag is also not rendering?

Greetings, thanx in advance!

If I don’t fill in the field in the panel

Do you mean extract()?

Unfortunately, I didn’t understand your question 100%. For a better understanding you could post the code from the blueprint. But maybe my solution already fits:

You need to expand your if query whether heading() OR subheading() is also empty.

<?php if ($page->extract()->isEmpty() && $page->heading()->text()->isEmpty() || $page->subheading()->text()->isEmpty()): ?>

    <section>
        <p><?= $page->heading()->text() ?></p>
        <p><?= $page->subheading()->text() ?></p>
    </section>

<?php endif ?>

If heading() AND subheading() must be empty, replace || (or) with && (and)

My bad. I mean that when that paragraph is not filled in in the panel. I want the surrounding tag also not to be rendered. So in this case the “section” tag.

  • So when you dont fill in the field for P in the panel
  • Kirby should rendrr no “section” and also no ”p”

Is this possible?

Can you please post the code from your blueprint for a better understanding.

If you only want to check the two fields heading() and subheading() for content, the shortened query is sufficient:

<?php if ($page->heading()->text()->isNotEmpty() && $page->subheading()->text()->isNotEmpty()): ?>

    <section>
        <p><?= $page->heading()->text() ?></p>
        <p><?= $page->subheading()->text() ?></p>
    </section>

<?php endif ?>

In this case, <section> is displayed if both fields are not empty.
If you want <section> to be displayed if only one of the two fields is empty, use this code:

<?php if ($page->heading()->text()->isNotEmpty() || $page->subheading()->text()->isNotEmpty()): ?>

    <section>
        <p><?= $page->heading()->text() ?></p>
        <p><?= $page->subheading()->text() ?></p>
    </section>

<?php endif ?>

My bad. I will clarify.
Forgot the 2 fields.

We have:
section with paragraph inside.

if paragraph = empty then don’t display it + the parent section. (So when empty don’t render anything)
That is what I need if that is possible.

Please post your blueprint

title: bookings

columns:
  main:
    width: 1/2
    sections:
      fields:
        type: fields
        fields:
          heading:
            type: text
          subheading:
            type: text

To be clear: its just the tag in the HTML that I don’t want to be rendered if one or both of the fields is not filled in.

So that the whole section on the frontend is gone if user does not fill it in

<?php if ($page->heading()->isNotEmpty() || $page->subheading()->isNotEmpty()): ?>

    <section>
        <p><?= $page->heading() ?></p>
        <p><?= $page->subheading()?></p>
    </section>

<?php endif ?>

To prevent the empty p tags within the collection if one of the fields subheading or heading is empty, you can do one of two things:

  1. Use kirbytext for the output to wrap the field content in p tags:
<?php if ($page->heading()->isNotEmpty() || $page->subheading()->isNotEmpty()): ?>

    <section>
        <?= $page->heading()->kt() ?>
        <?= $page->subheading()->kt() ?>
    </section>

<?php endif ?>
  1. Use additional if statements
<?php if ($page->heading()->isNotEmpty() || $page->subheading()->isNotEmpty()): ?>

  <section>
    <?php if ($page->heading()->isNotEmpty(): ?>
    <p><?= $page->heading() ?></p>
    <?php endif ?>
    <?php if ($page->subheading()->isNotEmpty(): ?>
    <p><?= $page->subheading() ?></p>
    <?php endif ?>
</section>

<?php endif ?>

For headings, however, hx tags would be more appropriate than p tags…

1 Like

Thanx so much!!