Categories + link to category overview

I’ve been looking into displaying the categories of the current blog post. (partly because I’d like to use them in a table partly to work on my noob status in xwiki)

I can display them using $doc.display(‘category’) , this however prefixes the class they belong to

image

To make matters worse I’m fairly certain there’s already a macro for this, as the footer of the BlogPostClass already shows a clickable variant of what I’m looking for!

image

despite my best efforts I have yet to find how this is done though, can someone help me on my way either to figure out how to unpack the class prefix or how to acces the footer macro?

I think you are looking for BlogCode.xml line 876.
You might even be able to reuse the listCategories velocity macro.

1 Like

Thanks a heap once more @mleduc , that resource was very illuminating and got me going quickly

TIL:
-how to read the XWiki Scripting API Reference (XWiki.org)
-took some steps in velocity (minor, undoubtedly tiny to you all here)
-the possibility of $foreach.count
-a whole bunch about .getDocument() and classes

I ended up going with :

{{velocity}}
#foreach ($category in $doc.getValue('category'))
  #if($foreach.count == 1)
      [[$xwiki.getDocument($category).name>>$category]] 
  #else
      ,[[$xwiki.getDocument($category).name>>$category]]
  #end
#end
{{/velocity}}

I ended up having to smush it all in one line to get it neatly in a table, is there a way around that to keep the code more readable? (so that the output is gathered)

i.e. this is whats in my implementation ( | for the table entry);

#foreach ($category in $doc.getValue('category'))
#if($foreach.count == 1)
|[[$xwiki.getDocument($category).name>>$category]] #else,[[$xwiki.getDocument($category).name>>$category]]
#end
#end

either way thanks a heap!
image

1 Like

You can avoid putting everything in one line by ending up a line with a comment (##).

For instance:

{{velocity}}
#set ($items = [1,2,3])

#foreach ($item in $items)
  #if($foreach.count == 1)
      $item
  #else
      , $item
  #end
#end

#foreach ($item in $items)
  #if($foreach.count == 1)
      $item##
  #else
      , $item##
  #end
#end
{{/velocity}}

Will output:

1
, 2
, 3

1, 2, 3

You can see that in the second foreach, all the values are printed without line-breaks.

1 Like

Love it, very insightfull thanks a ton once more @mleduc !