Generate ordered list of pages within a top-level folder

29 views
Skip to first unread message

Mark Pitely

unread,
Nov 20, 2017, 1:59:53 PM11/20/17
to dot...@googlegroups.com
There's been threads about this, as this is probably a pretty common task, but it is extremely difficult.
Here's code to generate a list of all the html files *in order* within a given folder, including all the subfolders. I added some pretty exhaustive comments at the end: too often I just post code instead of trying to properly explain things, and for new users there are so many little painful gotchas.

#set ($topurl="home")
#set($_qk = "+contentType:htmlpageasset  +live:true +working:true  +_all:/${topurl}*")
#set ($pagepull=$dotcontent.pull($_qk,0,'htmlpageasset.url'))
#set ($total=$pagepull.size())
#set($pagerArray = $contents.getEmptyList())
#foreach ($pager in $pagepull)
#set($pagerObject = $contents.getEmptyMap())
#set($_dummy=$pagerObject.put("url",$pager.url))
#set($_dummy=$pagerObject.put("title",$pager.title))
#set($_dummy=$pagerObject.put("pathcount",$pager.url.lastIndexOf('/')))
#set($_dummy=$pagerObject.put("pathdepth",$pager.url.split("\/").size()))
#set($_dummy=$pagerArray.add($pagerObject))
#end
#set ($sorted=$sorter.sort($pagerArray,["pathdepth:asc","url:asc"]))


#foreach ($pager in $sorted)
<a href="$pager.url">$pager.title</a> $pager.url.substring(0,$pager.pathcount) $pager.pathdepth <br>
#end

Comments:
$topurl in this case is something like 'home' (note, I add the leading slash in the pull, modify as needed, also the trailing asterisk is necessary to get the deeper folders)

dotcontent.pull's resulting list (the normal pull result you can usually use) is sorted by the html file name, ignoring the folder/path. So, not useful and essentially random. Any use of folder gives an inode. So there's a pretty terrible roadblock to getting what you need or want, sorted.
What I did here is hand-build my own array of maps and used the velocity sorting tool to better get my sort. (The end $sorted behaves like the content you are used to working with)

$pager.url here renders as the full path despite the dotCMS field url not actually having that information; at some point the dotcontent.pull must resolve that, hidden.

pathdepth is a little of a cheat, as I do a split on /, but then just get the size - basically an integer that tells me how many slashes are in the path, which is the key element in sorting things by folder. (Note you have to escape the / in split, but not in lastIndexOf) [Bonus note: because split will accept a regular expression]
That is: /x/zzz.html comes before /x/a/aaa/html.

pathcount just tells me where the last slash is, so I can break url into filename and filepath.

The (#set ($_dummy ... is a bit different than other velocity. The .put command normally echoes to the screen, so assigning it to $_dummy just suppresses it, you could use any variable there)
Using the _ as part of the variable is just a habit I picked up from someone here (Keiter?) to show it is a dummy/throwaway variable.

Lastly, the $sorter.sort takes its sorting list a little differently, note the ["..."] inside the command. (if you give just "pathdepth:asc" it works without the array brackets, but you need them for the second sort element. )

Exceptions and inconsistencies galore!


Mark Pitely
Marywood University
Reply all
Reply to author
Forward
0 new messages