I have a clientlib path which is getting generated in <script> tag by using data-sly-call to a specific clientlib category in HTL file
Is there any way that we can get this etc.clientlib path with the proper id in Sling model?
Solved! Go to Solution.
Views
Replies
Total Likes
Hi @JavedKh
Yes, there are ways to generate the correct clientlib URL in your sling model using AEM API's,
Instead of HTL constructs.
You can inject the HtmlLibraryManager OSGi service into your Sling Model and call its getLibraryUrl method.
import com.adobe.granite.ui.clientlibs.HtmlLibraryManager;
import com.adobe.granite.ui.clientlibs.LibraryType;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.Self;
import org.apache.sling.models.annotations.injectorspecific.OSGiService;
import javax.annotation.PostConstruct;
@Model(adaptables = ResourceResolver.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class ClientLibPathModel {
@Self
private ResourceResolver resourceResolver;
@OSGiService
private HtmlLibraryManager htmlLibraryManager;
private String clientlibPath;
@PostConstruct
protected void init() {
if (htmlLibraryManager != null) {
clientlibPath = htmlLibraryManager.getLibraryUrl(resourceResolver, "category-name", LibraryType.JS);
}
}
public String getClientlibPath() {
return clientlibPath;
}
}
Hope this helpful....:)
Regards,
Karishma.
Hi @JavedKh
Yes, there are ways to generate the correct clientlib URL in your sling model using AEM API's,
Instead of HTL constructs.
You can inject the HtmlLibraryManager OSGi service into your Sling Model and call its getLibraryUrl method.
import com.adobe.granite.ui.clientlibs.HtmlLibraryManager;
import com.adobe.granite.ui.clientlibs.LibraryType;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.Self;
import org.apache.sling.models.annotations.injectorspecific.OSGiService;
import javax.annotation.PostConstruct;
@Model(adaptables = ResourceResolver.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class ClientLibPathModel {
@Self
private ResourceResolver resourceResolver;
@OSGiService
private HtmlLibraryManager htmlLibraryManager;
private String clientlibPath;
@PostConstruct
protected void init() {
if (htmlLibraryManager != null) {
clientlibPath = htmlLibraryManager.getLibraryUrl(resourceResolver, "category-name", LibraryType.JS);
}
}
public String getClientlibPath() {
return clientlibPath;
}
}
Hope this helpful....:)
Regards,
Karishma.