Expand my Community achievements bar.

SOLVED

HTL Variables

Avatar

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

Avatar

Correct answer by
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>

 



Arun Patidar

View solution in original post

1 Reply

Avatar

Correct answer by
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>

 



Arun Patidar