Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ##This macro will loop through all the contentlets in a query,
- ##and create a list of all of tags and how many times they occur
- ##ie. [{title=shoes, count=4}, {title=achilles, count=1}, {title=cara, count=6}]
- ##
- ## This list can then be sorted and thrown back into the page
- #### MACRO CODE
- #macro (tagListByQuery $query $fieldname)
- #set($tagMap = $contents.getEmptyMap())
- #foreach($content in $dotcontent.pull($query,0,"modDate desc"))
- #foreach($tag in $content.get($fieldname))
- #if(!$tagMap.containsKey($tag)) ## If the list doesn't have my tag already, add it
- #set($dummy = $tagMap.put($tag, 1))
- #else ## If the list has my tag, increment the count value
- #set($currentCount = $tagMap.get($tag))
- #set($newCount = $math.add($currentCount, 1))
- #set($dummy = $tagMap.put($tag, $newCount))
- #end
- #end
- #end
- ## Shuffle everything across into a list where it can be sorted
- #set($tagList = $contents.getEmptyList())
- #foreach($tag in $tagMap.keySet())
- #set($item = $contents.getEmptyMap())
- #set($item = $contents.getEmptyMap())
- #set($dummy = $item.put("title",$tag))
- #set($dummy = $item.put("count",$tagMap.get($tag)))
- #set($dummy = $tagList.add($item))
- #end
- #end
- #### USAGE EXAMPLE
- #tagListByQuery ("+structureName:BlogEntry +live:true" "tag")
- <ul>
- #foreach ($tag in $sorter.sort($tagList,["count:desc", "title:asc"]))
- <li><strong>$tag.title</strong> - $tag.count</li>
- #end
- </ul>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement