Expand my Community achievements bar.

Get ready! An upgraded Experience League Community experience is coming in January.
SOLVED

how to hide div for unauthored parsys

Avatar

Level 3
<div class="xyz-subscribe xyz-container xyz-social">
<sly data-sly-resource="${'xyzsubscribeshare' @ resourceType='wcm/foundation/components/responsivegrid'}"/>
</div>

i have this code to add two components,
while authoring, authors are adding components without authoring due to it, we can see additional div's are showing in published page.
how to avoid it?
1 Accepted Solution

Avatar

Correct answer by
Level 10

hi @SudarshanV1

This removes the outer wrapper div but does not stop authors from adding components:

<sly data-sly-resource="${'xyzsubscribeshare' 
    @ resourceType='wcm/foundation/components/responsivegrid', decoration=false}"/>

 

Below is the standard and clean AEM pattern for hide component on Publish if required fields are missing by using a Sling Model method like isConfigured() + HTL conditional rendering.
Create Sling Model with isConfigured() method:

package com.xyz.core.models;

import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.ValueMapValue;

@Model(
    adaptables = Resource.class,
    defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL
)
public class SubscribeShareChildModel {

    @ValueMapValue
    private String title;

    @ValueMapValue
    private String link;

    public String getTitle() {
        return title;
    }

    public String getLink() {
        return link;
    }

    /** 
     * Component should render only if required fields are filled 
     */
    public boolean isConfigured() {
        return title != null && !title.isEmpty()
            && link != null && !link.isEmpty();
    }
}

Then use isConfigured() in HTL to hide the component:

<sly data-sly-use.model="com.xyz.core.models.SubscribeShareChildModel" data-sly-test="${model.configured}">
    <!-- Render component only when configured correctly -->
    <div class="subscribe-share">
        <h3>${model.title}</h3>
        <a href="${model.link}">Subscribe</a>
    </div>
</sly>

<sly data-sly-test="${!model.configured && wcmmode.edit}">
    <div class="cq-placeholder">Component not configured</div>
</sly>

When the component is not configured, nothing appears on Publish, no empty divs, no layout pollution.

View solution in original post

1 Reply

Avatar

Correct answer by
Level 10

hi @SudarshanV1

This removes the outer wrapper div but does not stop authors from adding components:

<sly data-sly-resource="${'xyzsubscribeshare' 
    @ resourceType='wcm/foundation/components/responsivegrid', decoration=false}"/>

 

Below is the standard and clean AEM pattern for hide component on Publish if required fields are missing by using a Sling Model method like isConfigured() + HTL conditional rendering.
Create Sling Model with isConfigured() method:

package com.xyz.core.models;

import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.ValueMapValue;

@Model(
    adaptables = Resource.class,
    defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL
)
public class SubscribeShareChildModel {

    @ValueMapValue
    private String title;

    @ValueMapValue
    private String link;

    public String getTitle() {
        return title;
    }

    public String getLink() {
        return link;
    }

    /** 
     * Component should render only if required fields are filled 
     */
    public boolean isConfigured() {
        return title != null && !title.isEmpty()
            && link != null && !link.isEmpty();
    }
}

Then use isConfigured() in HTL to hide the component:

<sly data-sly-use.model="com.xyz.core.models.SubscribeShareChildModel" data-sly-test="${model.configured}">
    <!-- Render component only when configured correctly -->
    <div class="subscribe-share">
        <h3>${model.title}</h3>
        <a href="${model.link}">Subscribe</a>
    </div>
</sly>

<sly data-sly-test="${!model.configured && wcmmode.edit}">
    <div class="cq-placeholder">Component not configured</div>
</sly>

When the component is not configured, nothing appears on Publish, no empty divs, no layout pollution.