This blog is about how can we stop rendering of empty html tags in Sitecore SXA. Thank you Nathan Hase for your suggestion to use Rule Engine.
We have hero component which has following rendering/html structure.
Now if you notice the Hero Headline, it has a wrapper div. So, the title will be rendered in following html structure.
Now what if there is no content in Headline field. The same structure will be rendered without Headline value.
You can see, empty h1 tag and its parent div tag. And we don’t want that.
Sitecore SXA provides OOTB solution for this.
We need to update two things:
- The field itself i.e. Variant field.
- Its parent div which is wrapper/Variant Section
If you select Variant field, you can see checkbox Render if empty in Variant Details Section.
Make sure it is not checked/ticked. This means, it will not render – if it is empty (means field-name mentioned in this Variant Field is not having any value).
Now let’s update wrapper/variant section i.e. HeaderWrapper:
- Select the Variant Section item.
- You can see Rules section. It means that this Variant Section will be enabled only if the mentioned rule is true. Click on Edit rule.
- Search for field and under Fields related rule, click on “where the specific field is empty” rule.
- In the selected rule, click on “specific” text and enter the field name (in this context it is HeroHeadline).
- Now this means, if HeroHeadline field is empty, section will be enabled. But we want a rule which is completely opposite. Click on “where” and it will get converted into “except where”.
So now, our rule is correct i.e. Enable the section except where the “HeroHeadline” field is empty.You can add multiple rules.
- Publish the Variant.
Now, empty h1 tag and its parent div tag will not get rendered.
Happy Coding 🙂