| ... |
... |
@@ -7,6 +7,19 @@ |
| 7 |
7 |
#set($featuredFilter = [1, 0]) |
| 8 |
8 |
#end |
| 9 |
9 |
#set($collabs = $query.bindValue('featured', $featuredFilter).execute()) |
|
10 |
+#macro(collablist $collabs) |
|
11 |
+[ |
|
12 |
+#foreach($collab of $collabs) |
|
13 |
+ #set($highlight = $xwiki.getDocument($collab)) |
|
14 |
+ #set($collabId = $highlight.getValue('collab_id')) |
|
15 |
+ #set($collabURL = '/bin/view/Collabs/'+$collabId) |
|
16 |
+ #set($hasPicture = $highlight.getAttachment('highlight.png').isImage()) |
|
17 |
+ { |
|
18 |
+ "title": "$highlight.getTitle()" |
|
19 |
+ }, |
|
20 |
+#end |
|
21 |
+] |
|
22 |
+#end |
| 10 |
10 |
{{/velocity}} |
| 11 |
11 |
|
| 12 |
12 |
{{velocity wiki="false"}} |
| ... |
... |
@@ -14,24 +14,18 @@ |
| 14 |
14 |
#if("$!request.outputSyntax" != '') |
| 15 |
15 |
$response.setContentType('application/json') |
| 16 |
16 |
#end |
|
30 |
+##set($MyJsonData = {'greeting': 'Hello', 'location': 'World'}) |
|
31 |
+##$jsontool.serialize($collabs) |
| 17 |
17 |
|
| 18 |
|
-#foreach($collab of $collabs) |
| 19 |
|
- #set($highlight = $xwiki.getDocument($collab)) |
| 20 |
|
- #set($collabId = $highlight.getValue('collab_id')) |
| 21 |
|
- #set($collabURL = '/bin/view/Collabs/'+$collabId) |
| 22 |
|
- #set($hasPicture = $highlight.getAttachment('highlight.png').isImage()) |
| 23 |
|
- #if($hasPicture) |
| 24 |
|
- #set($picture = $highlight.getAttachmentURL('highlight.png')) |
| 25 |
|
- #else |
| 26 |
|
- #set($picture = "") |
| 27 |
|
- #end |
|
33 |
+$jsontool.serialize(#collablist($collabs)) |
| 28 |
28 |
|
| 29 |
|
- { |
| 30 |
|
- "title": "$highlight.getTitle()", |
| 31 |
|
- "content": "$highlight.getContent()", |
| 32 |
|
- "image": "$picture", |
| 33 |
|
- }#if( $foreach.hasNext ), #end |
| 34 |
|
-#end |
| 35 |
|
-]) |
|
35 |
+## In order to ensure the generated JSON is valid it is recommanded to |
|
36 |
+## build the JSON in memory (using Velocity/Java maps, arrays and basic types) |
|
37 |
+## and then serialize it with $jsontool.serialize method. |
|
38 |
+## Usage (from Marius: http://xwiki.markmail.org/message/wsvaj4leg5atufwd): |
|
39 |
+## #set($array = [1, 2, 3]) |
|
40 |
+## $jsontool.serialize($array) |
|
41 |
+## #set($map = {'enabled': true, 'color': 'yellow', 'count': 13}) |
|
42 |
+## $jsontool.serialize($map) |
| 36 |
36 |
|
| 37 |
37 |
{{/velocity}} |