HTL Variables | Community
Skip to main content
lisa_burrow
Level 2
August 2, 2021
Solved

HTL Variables

  • August 2, 2021
  • 1 reply
  • 774 views

Hi,

 

I am new to HTL and wondered if you are able to manipulate the data that is returned from a dialog?

 

Basically I am doing:

<div data-sly-test.leftURLExists="${properties.leftURL}">
   <sly data-sly-set.leftURLValue="${properties.leftURL}"></sly>
</div>

 

I need to check the value inside 'leftURLValue' to see what it starts with. If it starts, https:// I need to do something, however, if it starts /content/ I need to do something else with it.

 

Can this be done in HTL?

 

If not, can I pass this 'value' to a .js file from inside HTL? I can then manipulate the data in the .js file to check for https:// or /content/, then return the results to  the HTL file to print out the result on my page. If this is the case please can someone help with this?

 

Thanks Lisa.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by arunpatidar

Hi,

HTL does not support the matching function unlike String.

You have to check and manipulate urls at backed(Use JS or Java Sling Model) and return to HTL.

 

e.g.

 

import javax.annotation.PostConstruct; import javax.inject.Inject; import org.apache.sling.api.resource.Resource; import org.apache.sling.models.annotations.DefaultInjectionStrategy; import org.apache.sling.models.annotations.Model; @Model(adaptables = Resource.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL) public class LinkModel { @Inject public String lnLabel, lnHref, lnTarget; @PostConstruct protected void init() { if (lnHref != null) { if (lnHref.startsWith('/content')) { // do something e.g. lnHref = lnHref + ".html"; } else{ // do something else } } } public String getLnLabel() { return lnLabel; } public String getLnHref() { return lnHref; } public String getLnTarget() { return lnTarget; } }

 

 

 

<sly data-sly-use.link="com.myproj.core.models.LinkModel" <a href="${link.lnHref}" target="${link.lnTarget}" >${link.lnLabel} </a> </sly>

 

1 reply

arunpatidar
Community Advisor
arunpatidarCommunity AdvisorAccepted solution
Community Advisor
August 2, 2021

Hi,

HTL does not support the matching function unlike String.

You have to check and manipulate urls at backed(Use JS or Java Sling Model) and return to HTL.

 

e.g.

 

import javax.annotation.PostConstruct; import javax.inject.Inject; import org.apache.sling.api.resource.Resource; import org.apache.sling.models.annotations.DefaultInjectionStrategy; import org.apache.sling.models.annotations.Model; @Model(adaptables = Resource.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL) public class LinkModel { @Inject public String lnLabel, lnHref, lnTarget; @PostConstruct protected void init() { if (lnHref != null) { if (lnHref.startsWith('/content')) { // do something e.g. lnHref = lnHref + ".html"; } else{ // do something else } } } public String getLnLabel() { return lnLabel; } public String getLnHref() { return lnHref; } public String getLnTarget() { return lnTarget; } }

 

 

 

<sly data-sly-use.link="com.myproj.core.models.LinkModel" <a href="${link.lnHref}" target="${link.lnTarget}" >${link.lnLabel} </a> </sly>

 

Arun Patidar