Categories sort order (parent-child)

70 views
Skip to first unread message

aDNers

unread,
Sep 11, 2017, 4:15:10 AM9/11/17
to dotCMS User Group
Hello,

I almost feel a bit stupid asking this but does anyone know if it possible to control the order of the results returned by a loop of the categories for a given content? I am trying to return the categories in a hierarchical order with the parent first followed by the children. Using the following foreach-loop I get, what it feels like, a totally random sort order of the categories.

#foreach($cat in $test.categories)
<li><b>Category Name:</b> $!{cat.categoryName}</li>
<ul>
<li><i>Category Key:</i> $!{cat.key}</li>
<li><i>Velocity Variable Name:</i> $!{cat.categoryVelocityVarName}</li>
</ul>
#end

I found ways of returning both the children of a given parent, aswell as the parent categories of a given child - but just listing the categories for a given content seems totally random to me. Sometimes I get the child first, and in other cases the parent is returned first - and I don't understand why.)

Searching the forum I found this similar (unanswered) post from 2015 (https://groups.google.com/forum/embed/?place=forum/dotcms&showsearch=true&showpopout=true&parenturl=https%3A%2F%2Fdotcms.com%2Fforum%2F#!searchin/dotcms/categories$20sort/dotcms/Q28HmRysQSE/8RaX5lkf-KkJ) aswell as another post where using the Sorttool was suggested: 


Thanks in advance,

aDNers

unread,
Sep 11, 2017, 9:52:33 AM9/11/17
to dotCMS User Group
Hey, I was able to create a workaround. 

For each $cat in $test.categories I now check if the category has any children, if so I take the catkey and use the  getCategoryTreeUp to list all parents. This way I have control of the order in the output. Works for my current situation, but I am still curious if it is possible to have control over the category order somehow.

Mark Pitely

unread,
Sep 11, 2017, 10:49:41 AM9/11/17
to dot...@googlegroups.com
There is a built-in sorting viewtool that allows you to sort certain types of lists/arrays.
I don't see any documentation anymore for it, though, so I am afraid they may be in the process of deprecating it. It still works, last I used it (3.6).

You would simply do something like this:

#set ($sortedcategories=$sorter.sort($test.categories,"key:desc"))

#foreach ($test in $sortedcategories)
...
#end

Aside from that, I don't know a good solution to your issue.
Maybe someone else knows something, especially on the status of the viewtool.

Mark Pitely
Marywood University




--
http://dotcms.com - Open Source Java Content Management
---
You received this message because you are subscribed to the Google Groups "dotCMS User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dotcms+unsubscribe@googlegroups.com.
To post to this group, send email to dot...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/dotcms/916d78b6-8cda-49b7-9f4e-c1924d9aa2fe%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Nathan Keiter

unread,
Sep 11, 2017, 11:10:09 AM9/11/17
to dot...@googlegroups.com

Looks like it is still part of the toolbox in dotCMS 4.1.1 Additionally, it is a generic Velocity plugin, not a dotCMS specific tool.

 

https://github.com/dotCMS/core/blob/4.1.1/dotCMS/src/main/webapp/WEB-INF/toolbox.xml#L52-L56

 

Nathan I. Keiter | Lead Network Applications Programmer | Benefits Advisory Council Member | I.D.E.A Council Member
Gettysburg College | Information Technology | DataSystems
Campus Box 2453 | 300 North Washington Street | Gettysburg, PA 17325
Phone: 717.337.6993

https://www.gettysburg.edu

To unsubscribe from this group and stop receiving emails from it, send an email to dotcms+un...@googlegroups.com.


For more options, visit https://groups.google.com/d/optout.

 

--

http://dotcms.com - Open Source Java Content Management
---
You received this message because you are subscribed to the Google Groups "dotCMS User Group" group.

To unsubscribe from this group and stop receiving emails from it, send an email to dotcms+un...@googlegroups.com.


To post to this group, send email to dot...@googlegroups.com.

aDNers

unread,
Sep 11, 2017, 11:22:11 AM9/11/17
to dotCMS User Group
Nathan, Mark:

Thanks for your replies. Will look into the sorting plugin aswell, even though my "workaround" actually seems to do the job for me at the moment.

Br
Anders

To post to this group, send email to do...@googlegroups.com.


For more options, visit https://groups.google.com/d/optout.

Mark Pitely

unread,
Sep 11, 2017, 12:01:27 PM9/11/17
to dot...@googlegroups.com
Nathan,

Thanks! That reassures me. There is no documentation for it at all on dotCMS's site, at least that I can find.
It's a pretty nice tool, too.
They have documentation for $math, etc, which I think is another generic. You would think it would live there.


Mark

To unsubscribe from this group and stop receiving emails from it, send an email to dotcms+unsubscribe@googlegroups.com.

To post to this group, send email to dot...@googlegroups.com.

John Michael Thomas

unread,
Sep 11, 2017, 12:09:24 PM9/11/17
to dotCMS User Group
Yeah, apologies that there are still a few of the Apache velocity viewtools we haven't yet created our own version of docs for.

Reply all
Reply to author
Forward
0 new messages