Advertisement
stephenjbell

List of tags for a specific query - (dotCMS)

Apr 1st, 2011
440
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.44 KB | None | 0 0
  1. ##This macro will loop through all the contentlets in a query,
  2. ##and create a list of all of tags and how many times they occur
  3. ##ie. [{title=shoes, count=4}, {title=achilles, count=1}, {title=cara, count=6}]
  4. ##
  5. ## This list can then be sorted and thrown back into the page
  6.  
  7.  
  8. #### MACRO CODE
  9.  
  10. #macro (tagListByQuery $query $fieldname)
  11.     #set($tagMap = $contents.getEmptyMap())
  12.     #foreach($content in $dotcontent.pull($query,0,"modDate desc"))
  13.         #foreach($tag in $content.get($fieldname))
  14.             #if(!$tagMap.containsKey($tag))  ## If the list doesn't have my tag already, add it
  15.                 #set($dummy = $tagMap.put($tag, 1))
  16.             #else ## If the list has my tag, increment the count value
  17.                 #set($currentCount = $tagMap.get($tag))
  18.                 #set($newCount = $math.add($currentCount, 1))
  19.                 #set($dummy = $tagMap.put($tag, $newCount))
  20.             #end
  21.         #end
  22.     #end
  23.    
  24.     ## Shuffle everything across into a list where it can be sorted
  25.     #set($tagList = $contents.getEmptyList())
  26.     #foreach($tag in $tagMap.keySet())
  27.         #set($item = $contents.getEmptyMap())
  28.         #set($item = $contents.getEmptyMap())
  29.         #set($dummy = $item.put("title",$tag))
  30.         #set($dummy = $item.put("count",$tagMap.get($tag)))
  31.         #set($dummy = $tagList.add($item))
  32.     #end
  33. #end
  34.  
  35.  
  36. #### USAGE EXAMPLE
  37.  
  38. #tagListByQuery ("+structureName:BlogEntry +live:true" "tag")
  39.  
  40. <ul>
  41. #foreach ($tag in $sorter.sort($tagList,["count:desc", "title:asc"]))
  42.     <li><strong>$tag.title</strong> - $tag.count</li>
  43. #end
  44. </ul>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement