This conversation has been locked due to inactivity. Please create a new post.
This conversation has been locked due to inactivity. Please create a new post.
I've got a property as follows
items="[{"linkLabel":"Monkey"\,"anchor":"#monkey"}, "linkLabel":"Bear"\,"anchor":"#bear"},{"linkLabel":"Lion"\,"anchor":"#lion"}]"
When I loop through it in Sightly I see the values:
Sightly Code:
<div data-sly-list.itemList="${properties.items}">
<div>item: ${itemList}</div>
</div>
Output:
item: {"linkLabel":"Monkey","anchor":"#monkey"}
item: {"linkLabel":"Bear","anchor":"#bear"}
item: {"linkLabel":"Lion","anchor":"#lion"}
How can I get the values of 'linkLabel' and 'anchor' individually?
Thank you in advance for your help.
-Dean
Solved! Go to Solution.
I wrote a class to handle this.
package com.project.sightly; import com.adobe.cq.sightly.WCMUse; import org.apache.sling.commons.json.JSONObject; import java.util.*; public class JsonHelper extends WCMUse { private String[] json; @Override public void activate() throws Exception { json = this.get("json", String[].class); } public List<Map<String, String>> getValues() { List<Map<String, String>> results = new ArrayList<Map<String, String>>(); if (json != null) { for (String value : json) { Map<String, String> column = parseItem(value); if (column != null) { results.add(column); } } } return results; } private Map<String, String> parseItem(String value) { Map<String, String> columnMap = new HashMap<String, String>(); try { JSONObject parsed = new JSONObject(value); for (Iterator<String> iter = parsed.keys(); iter.hasNext();) { String key = iter.next(); String innerValue = parsed.getString(key); columnMap.put(key, innerValue); } return columnMap; } catch (Exception e) { } return null; } }
You can use it like this:
<div data-sly-use.multi="${'com.project.sightly.MultiField' @ json=properties.items}"> <div data-sly-repeat="${multi.values}"> <h2>${item['linkLabel']} - ${item['anchor']}</h2> </div> </div>
Views
Replies
Total Likes
I wrote a class to handle this.
package com.project.sightly; import com.adobe.cq.sightly.WCMUse; import org.apache.sling.commons.json.JSONObject; import java.util.*; public class JsonHelper extends WCMUse { private String[] json; @Override public void activate() throws Exception { json = this.get("json", String[].class); } public List<Map<String, String>> getValues() { List<Map<String, String>> results = new ArrayList<Map<String, String>>(); if (json != null) { for (String value : json) { Map<String, String> column = parseItem(value); if (column != null) { results.add(column); } } } return results; } private Map<String, String> parseItem(String value) { Map<String, String> columnMap = new HashMap<String, String>(); try { JSONObject parsed = new JSONObject(value); for (Iterator<String> iter = parsed.keys(); iter.hasNext();) { String key = iter.next(); String innerValue = parsed.getString(key); columnMap.put(key, innerValue); } return columnMap; } catch (Exception e) { } return null; } }
You can use it like this:
<div data-sly-use.multi="${'com.project.sightly.MultiField' @ json=properties.items}"> <div data-sly-repeat="${multi.values}"> <h2>${item['linkLabel']} - ${item['anchor']}</h2> </div> </div>
Views
Replies
Total Likes
Thank you for your help. I've done something similar, just wish there was an easier way to handle this in Sightly.
I like the way you approached this with this "generic" class.
Thanks again.
-Dean
Views
Replies
Total Likes
In my view - (not everyone agrees) - making use of Java on back-end with use of Sightly gives you a lot of power instead of trying to duplicate syntax in Sightly. Let Java handle collections, etc.
<div data-sly-list.outerlistelement="${properties.items}"> <div data-sly-list.innerlistelement="${outerlistelement}" data-sly-unwrap> <div>linkLabel: ${innerlistelement.linkLabel} anchor: ${innerlistelement.anchor}</div> </div> </div>
Views
Replies
Total Likes
smacdonald2008 wrote...
In my view - (not everyone agrees) - making use of Java on back-end with use of Sightly gives you a lot of power instead of trying to duplicate syntax in Sightly. Let Java handle collections, etc.
deana66659071 I agree with smacdonald2008
though you can use the code provided my me. It will solve your problem.
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies
Views
Likes
Replies
Views
Likes
Replies