Hi,
I have written some logic in the slingmodel and also i tried using sightly but due to syntax issue might be it is not working. I have tried 2 ways but it is not working.
problem statment: if the url is internal then we need to add extension as html and if url is external we do not add any extension.
@Inject @Optional
private String path;
public boolean isexternalurl(){
return StringUtils.startsWith(path,"/content");
}
Sightly :
option 1:
<div data-sly-set.pathval="${model.path @extension='html'}">
</div>
<a href="${model.Isexternalurl ?'pathval':'model.path'}">
</a>
option 2:
<div data-sly-set.pathval="${model.path @extension='html'}">
</div>
<p><a href="${'/content/'in model.path ?'pathval':'model.path'}">Visit W3Schools.com!</a></p>
Solved! Go to Solution.
Views
Replies
Total Likes
Hi,
Try to get url from backend
example
/** * @param string url * @return string Formatted url */ public static String getLink(String url) { String link = url; if (url != null) { final String regex = "(http:\\/\\/|https:\\/\\/(www)?)([a-zA-Z0-9]+).[a-zA-Z0-9]*.[a-z]{3}.?([a-z]+)?"; final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE); final Matcher matcher = pattern.matcher(url); if (!(matcher.find() || url.startsWith("#") || url.equals("/") || url.contains("?") || url.toLowerCase() .matches(".*\\.(doc|docx|htm|html|odt|pdf|xls|xlsx|ods|ppt|pptx|txt|png|jpeg|jpg|xls|xlsx|svg|zip|shtml|json|log)$") || url.toLowerCase().startsWith("tel:") || url.toLowerCase().startsWith("mailto:"))) { link = link + ".html"; } } return link; }
Hi @savi50
Instead of having condition at sightly, you can write the logic at sling Model itself and just read the value in sightly.
you can try below and see if that works.
Sling Model:
@Inject @Optional
private String path;
public void setPath() {
path = if(path.startsWith("/content”))? path +”html” : path ;
}
public String getPath() {
return path;
}
Sightly:
<a href="${model.path'}"> Link Text
</a>
<div
I suggest you to write all the business logic in the sling model itself. So that you would only need to call the getter from your Sling Model, and the transformed url will be the final string that you will be using for the Url output.
Hi,
Try to get url from backend
example
/** * @param string url * @return string Formatted url */ public static String getLink(String url) { String link = url; if (url != null) { final String regex = "(http:\\/\\/|https:\\/\\/(www)?)([a-zA-Z0-9]+).[a-zA-Z0-9]*.[a-z]{3}.?([a-z]+)?"; final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE); final Matcher matcher = pattern.matcher(url); if (!(matcher.find() || url.startsWith("#") || url.equals("/") || url.contains("?") || url.toLowerCase() .matches(".*\\.(doc|docx|htm|html|odt|pdf|xls|xlsx|ods|ppt|pptx|txt|png|jpeg|jpg|xls|xlsx|svg|zip|shtml|json|log)$") || url.toLowerCase().startsWith("tel:") || url.toLowerCase().startsWith("mailto:"))) { link = link + ".html"; } } return link; }
Views
Likes
Replies