Embed list of subpages of one page into another page

Hi everyone!

How can I embed a list of subpages of any wiki page into another one, in a way that exports to PDF? I know about the “documentTree” Macro, but it does not work for PDF export as it seems to be dynamically generated, and I can’t use it inline with a velocity script.
My situation is this:
I have a number of pages starting with the same prefix. All of these pages have a subpage, starting with another defined prefix, but the rest of the title is the same. I’d like to reuse the same code for all pages, so I want to build the references on the fly. I know how to work with strings, so the first shouldn’t be a problem, but the document references give me headaches.
Now what I want is to display a list of the subpages of a subpage of the main page :slight_smile:.
E.g. I have this structure:

  • Prefix My Page
    • PrefixLong My Page
      • Subpage 1
      • Subpage 2
      • etc.

Inside of “Prefix My Page” I want to display the subpages of “PrefixLong MyPage” dynamically, I’d say with a velocity script. The best thing would be if I wouldn’t have to enter one static page name, but build the strings on the fly. So from “Prefix My Page” I can extract “My Page” and build the name of the Subpage, “PrefixLong My Page”.
And the whole thing should be exportable to PDF.
Can anyone please point me to the right direction? I’m not very familiar with velocity and XWiki yet (though the API is mighty!)

Thanks for any help.

The PDF export UI doesn’t currently support selecting children pages to export but you can do it through a hand-crafted URL, see https://www.xwiki.org/xwiki/bin/view/Documentation/UserGuide/Features/Exports#HPDFExport and especially the pages query string parameter. You have some examples on that page.

Thanks

Unfortunately I don’t see support for regexes when using the pages query string parameter FTM on https://www.xwiki.org/xwiki/bin/view/Documentation/UserGuide/Features/Exports#HPDFExport. That’s a pity and it wouldn’t be hard to implement that… Thus you may need to generate your URL using some velocity script for example

Uhh… I might have formulated my question a bit unclear: I don’t want to export multiple pages at once, I want to see a list of subpages inside another wiki page. This page in turn will be exported to PDF, and inside the PDF I want to see this list as well. So just a list with titles, no actual contents of the subpages.
However I haven’t even found a way to create a list of subpages of any wiki page and embed this list into another page (with velocity). Additionally, I’d like to do it dynamically (not $xwiki.getDocument("A.B.C")).

Actually you can also control what gets exported by overriding the pdf.vm template in a custom skin.

ok so that’s easy :slight_smile:

Just write some script to list them. For example https://snippets.xwiki.org/xwiki/bin/view/Extension/List%20Children%20Of%20Current%20Document

There’s simpler but this script should do it.

or https://snippets.xwiki.org/xwiki/bin/view/Extension/Children%20Page%20Tree

Note to xwiki devs: It would be interesting that the children macro (and more generally the document tree macro) had a fallback to list a certain number of items when exported to HTML (used by the PDF export): https://extensions.xwiki.org/xwiki/bin/view/Extension/Children%20Macro/

If you have only 1 level pages, then you can use:

{{velocity}}
#foreach ($child in $doc.children)
  #set ($childDoc = $xwiki.getDocument($child))
  * $childDoc.getDisplayTitle()
#end
{{/velocity}}
1 Like

Amazing, quick and easy without having to hassle with references etc.
That nearly does what I’d like to have, just one little thing is missing for my use case:

{{velocity}}
#foreach ($child in $doc.getChildren())
  #set ($childDoc = $xwiki.getDocument($child))
  #if ($childDoc.getName().startsWith("PrefixLong"))
     #foreach ($subchild in $childDoc.getChildren())
        * $subchild.getDisplayTitle()
     #end
  #end
#end
{{/velocity}}

However, it seems I can’t access “startsWith” of java.lang.String, so I’d like to use the $stringtool, but the documentation doesn’t work right now?!

You can :slight_smile: And it works fine…

Your issue is that you’re comparing the wrong value…

When in doubt just output the result in your page as in:

{{velocity}}
#foreach ($child in $doc.getChildren())
  #set ($childDoc = $xwiki.getDocument($child))
DEBUG: $childDoc.getName()
  #if ($childDoc.getName().startsWith("PrefixLong"))
     #foreach ($subchild in $childDoc.getChildren())
        * $subchild.getDisplayTitle()
     #end
  #end
#end
{{/velocity}}

And… tada… you’ll get “WebHome”. Which is the last part of the reference.

If you want to check just the last part if the reference before the webHome, you can write:

$childDoc.getDocumentReference().getParent().getName()

It’s working fine for me, see https://www.xwiki.org/xwiki/bin/view/ScriptingDocumentation/

What’s wrong?

FYI I’ve uodated the javadoc for Document#getName(), see https://github.com/xwiki/xwiki-platform/blob/c33266fd7b004e2fb3ab462ba54671924dddf984/xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/api/Document.java#L275

Yeah, I replied before debugging, too impatient :wink:

I saw you noticed the issue (on IRC) as well, some list items are not clickable. Though I don’t need the stringtool anymore.

It’s clearer now, yes. Saves you two minutes of debugging…

Everything that I wanted works now, thanks for your quick help. I’ve now added links to the subpages, then put the whole script into a small wiki page. Now I can {{include.../}} this page in every page which needs it. And I can export the whole thing to PDF.