Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

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

@LokeshVajrala 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

3 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

Level 6

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

@LokeshVajrala 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