Expand my Community achievements bar.

Enhance your AEM Assets & Boost Your Development: [AEM Gems | June 19, 2024] Improving the Developer Experience with New APIs and Events
SOLVED

Sly test condition for 2 textfields

Avatar

Level 1

Dear Community,

 

I wanted to write a code where we will have a 2 textfields in aem

A textfield and B textfield

 

if i enter something in A textfield it will render on the page

if i keep blank in A textfield and will enter something in B textfield. B textfield value should render on the page

 

can you please help me. TIA.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@Lokesh_Vajrala Since we are using 

defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL

there is no need to check  

if (StringUtils.isNotBlank(textfieldA)) {
            return textfieldA;
        }

HTL Engine evaluates it internally. 

Please check this document for more details: https://sling.apache.org/documentation/bundles/models.html

View solution in original post

4 Replies

Avatar

Community Advisor

Hi @sasi1,

I would manage this with the implementation of Sling Model, Kindly refer below code.

Sling Model

@Model(adaptables = Resource.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL) 
public class DemoModel { 
      @Inject private String textfieldA; 
      @Inject private String textfieldB;

public String getTextfieldA() { returntextfieldA; }

public String getTextfieldB() { returntextfieldB; } }

HTL

<sly data-sly use.model="com.models.DemoModel" />
      <div>${model.textfieldA}></div>
      <div data-sly-test="${!model.textfieldA}">${model.textfieldB}></div>

or you can use HTL directly as below

<div>
      <div>${properties.textfieldA}></div>
      <div data-sly-test="${!properties.textfieldA}">${properties.textfieldB}></div>
</div>

Hope that helps!

Regards,

Santosh

Avatar

Community Advisor

Slightly better version - handle the logic in the Sling Model and return text value to render through HTL, so you will not see empty <div> elements in the generated markup.

@Model(adaptables = Resource.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class DemoModel {
    @Inject 
    private String textfieldA;
    @Inject 
    private String textfieldB;

    public String getText() {
        // textfieldA is not null and not empty, return textfieldA and don't look for textfieldB value
        if (StringUtils.isNotBlank(textfieldA)) {
            return textfieldA;
        }
        // textfieldA is either null or empty, return textfieldB value if it is not null and not empty
        if (StringUtils.isNotBlank(textfieldB)) {
            return textfieldB;
        }
        return null;
    }
}

HTL 

<sly data-sly use.model="com.models.DemoModel" />
<div data-sly-test="${model.text}">${model.text}></div>

 

Avatar

Correct answer by
Community Advisor

@Lokesh_Vajrala Since we are using 

defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL

there is no need to check  

if (StringUtils.isNotBlank(textfieldA)) {
            return textfieldA;
        }

HTL Engine evaluates it internally. 

Please check this document for more details: https://sling.apache.org/documentation/bundles/models.html

Avatar

Level 3

Hi Santosh,

 

your suggestion was help full for my situation, how do we check for multiple objects.

 

<div data-sly-test="${!properties.textfieldA && !properties.textfieldB}"> 
${properties.textFieldC} </div>

would this work?

 

Thank You.