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
BedrockMission!

Learn more

View all

Sign in to view all badges

SOLVED

HTL Variables

lisa_burrow
Level 2
Level 2

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.

1 Accepted Solution
Arun_Patidar
Correct answer by
Community Advisor
Community Advisor

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>

 

View solution in original post

1 Reply
Arun_Patidar
Correct answer by
Community Advisor
Community Advisor

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>

 

View solution in original post