[Solved] Display a list of pages with a specific tag

Hello,

I am trying to display on a page the list of pages with a specific tag (here: ‘test’).

I succeeded with the following command.

{{velocity}}
#set ($list = $xwiki.tag.getDocumentsWithTag('test'))
#foreach($doc in $list)
  $doc
#end
{{/velocity}}

But I would restrict the display with only the title of the page and not the entire path.

Currently it display :

Wiki.Page1.Page2.TitleIWantToDisplay1.WebHome
Wiki.Page1.Page2.TitleIWantToDisplay2.WebHome
Wiki.Page1.TitleIWantToDisplay3.WebHome
Wiki.Page1.Page2.Page3.TitleIWantToDisplay4.WebHome

What is the code I have to add to do this?

Thank you :smile:

1 Like

getDocumentsWithTag gives you documents references. To get the actual document title you will have the get the actual document ($xwiki.getDocument($reference)) and ask it for the title ($document.getRenderedTitle()).

1 Like

Thanks :slight_smile:
I tried this

 {{velocity}}
    #set ($list = $xwiki.tag.getDocumentsWithTag('Flux Internes'))
    #foreach($doc in $list)
      #set ($ref = $xwiki.getDocument($reference))
    #set ($doc1 = $document.getRenderedTitle($ref))
    $doc1
    #end
    {{/velocity}}

But it display only “Webhome”.

And I would like to add the corresponding link, I don’t know how to it

Does not sounds like a recent version of XWiki. getRenderedTitle() gives you exactly the same thing as the displayed h1 title when you view the page (since that’s what is used there too), if there is no title it fallback on the page name and in recent version when the page name is “WebHome” it display the parent name.

In default XWiki syntax the link syntax is [[$label>>$reference]]. See http://www.xwiki.org/xwiki/bin/view/Documentation/UserGuide/Features/XWikiSyntax/?syntax=2.1&section=Links.

1 Like

Hi tmortagne,

i’m in version : XWiki Enterprise Jetty HSQLDB 8.4.4

I’m currently always stuck at the same point, I don’t how to do it, I tried many combinaisons, nothing work, I think I extract only the title of the page where the script is executed, but not on the page where I want to extract the title.

PS : When I wrote “TitleIWantToDisplay” in my first, post, I think it meant “Space” (after a lot of reading ^^)

Ok I read your script too fast actually (and you did not really followed what I indicated :slight_smile: ):

  • $xwiki.getDocument takes a reference and return a document
  • then you ask the title of that document

in your script you seems to be a bit lost between what getDocument() takes and what it returns. I don’t even know why you even have something since $document is not supposed to exist (you don’t set it anywhere), maybe you inherit this variable from somewhere else.

Something else which is very dangerous in your script: don’t redefine $doc, it’s the standard binding for the current document (plus as I said that list contains references, not document so it would make much more sens to call it $reference).

Ok, my bad. I’m a debutant ^^
I modified the script. I get the good links on each occurrence but the recovered title is “WebHome&showTranslations=false” for all.

Here is the new script :

#set ($list = $xwiki.tag.getDocumentsWithTag('Flux Internes'))
#set ($document = $xwiki.getDocument($reference))
#set ($label = $document.getRenderedTitle($document))
     #foreach($reference in $list)
     [[$label>>$reference]]
#end

To be sure I fully understand what’s happening :
$list gets all the documents with the tag ‘Flux Internes’
$document gets the documents with all the $reference in $list
$label gets the title of each documents retrievied by $document
Foreach documents with the tag ‘Flux Internes’ (i.e each documents retrieved by $list), it displays the title of each document (i.e $label) with the link of each document (i.e $reference). I’m right ? :slight_smile:

getRenderedTitle does not take any parameter (you are already calling it from the $document).

Ok, that what I thought, but when I tried differents codes, it display “$label” for each document instead of the title, because my variable is null (I guess)

Do I need to nest the queries ?

I try this one, I have an invalid syntax format error :

#set ($list = $xwiki.tag.getDocumentsWithTag('Flux Internes'))
     #foreach($reference in $list)  
     [[$document.getRenderedTitle($xwiki.getDocument($reference))>>$reference]]
#end

ENFIIIIIIIIN

I get what I want now :smiley:

#set ($list = $xwiki.tag.getDocumentsWithTag('Flux Internes'))
#foreach($reference in $list)
  #set ($document = $xwiki.getDocument($reference))
  #set ($label = $document.getTitle())
  [[$label>>$reference]]
#end 

I change getRenderedTitle, by getTitle and it works fine.

1 Like

Also note that if you’re interested you could always get titles in links (when no label is explicitly defined). See http://www.xwiki.org/xwiki/bin/view/Documentation/AdminGuide/Configuration/#HLinkLabelGeneration

Anyone got an idea how you can alphabetically order this list?

The provided solution indeed works, but I’m hoping someone can add some sort of sorting/order logic to the list.

1 Like

Hello there,

is there any further progress or information about the sorting question? I would like to sort the do=viewtag list, too. (Alphabetically or by date)

guys please start a new topic for new questions.

Seems like your question is generic and it’s how to sort a list of String in velocity.

I’ve published your snippet on https://snippets.xwiki.org/xwiki/bin/view/Extension/Display%20pages%20with%20a%20specific%20tag/

Please don’t hesitate to contribute snippets there. Also don’t hesitate to go on https://snippets.xwiki.org to see how to do tons of stuff with script :slight_smile:

1 Like