Reputation: 185
I am creating some pages in which image component is being used. I want to put mysitelogo.jpg(as default image), if image is not uploaded by user or image is not present.
below is exsiting code
String imgPath = properties.get(ybUrl, String.class);
if (imgPath != null && !imgPath.equals("")) {
%><div class="image"><img src="<%= imgPath %>" alt="" ></div><%
}
Upvotes: 0
Views: 1047
Reputation: 364
@Mayank,
The Sling ValueMap allows for specifying either a class to cast the returned value or a default value when retrieving a property. Replace your existing code with:
String imgPath = properties.get(ybUrl, "/some/path/mysitelogo.jpg");
%><div class="image"><img src="<%= imgPath %>" alt="" ></div><%
This will return the default value to your image (after you correct the path). Since the ValueMap will automatically return the default value if the retrieved value is null, you no longer have to do the null check.
Upvotes: 1