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.
Solved! Go to Solution.
Views
Replies
Total Likes
@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
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
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>
@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
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.
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies
Views
Likes
Replies