Custom sorting order
Is there a way to assign a custom sorting order to my section array? Currently, my list is alphabetical, however I require a more specific order.
#set( $pageTitle = $_XPathTool.selectSingleNode($contentRoot, "//system-page[@current]/title") )
#set ( $sectionTitle = $_XPathTool.selectSingleNode($contentRoot, "//system-page[@current]/system-data-structure/sectionTitle") )
<h1>$_EscapeTool.xml($pageTitle.value)</h1>
#if($sectionTitle.value != "")
<h2 class="subtitle">$_EscapeTool.xml($sectionTitle.value)</h2>
#end
<hr />
#set( $sections = $_XPathTool.selectNodes($contentRoot, "//system-page/dynamic-metadata[name = 'position']") )
#if ( $sections.size() > 0 )
$_SortTool.addSortCriterion( "value", "en", "text", "ascending", "upper-first")
$_SortTool.sort($sections)
## Create an array of unique section values.
#set ( $sectionArr = [] )
#foreach ( $section in $sections )
#set ( $value = $section.getChild("value").value )
#if ( $sectionArr.indexOf($value) == -1 )
#set ( $_void = $sectionArr.add($value) )
#end
#end
## Loop over the unique sections and display the section and pages belonging to that section.
#foreach ( $section in $sectionArr )
#set($pages = $_XPathTool.selectNodes($contentRoot, "//system-page[name != 'index' and dynamic-metadata[name = 'position' and value = '${section}']]"))
<h2>${section}</h2>
#if ( $pages.size() > 0 )
#foreach ( $page in $pages )
#set( $name = $page.getChild('title').value )
#set( $data = $page.getChild('system-data-structure') )
#set( $title = $data.getChild('title').value )
#set( $image = $data.getChild('image').getChild('path').value )
#set( $contactInfo = $data.getChild('contactInfo') )
#set( $phone = $contactInfo.getChild('phone').value )
#set( $office = $contactInfo.getChild('office').value )
#set( $building = $contactInfo.getChild('building').value )
#set( $email = $contactInfo.getChild('email').value )
#set( $topics = $_XPathTool.selectNodes($data, "interests/topic") )
#set( $areas = $data.getChild('areas').value )
#set( $courses = $data.getChild('courses').value )
#set( $publications = $data.getChild('publications').value )
#set( $website = $page.getChild('link').value )
<div style="display:block;clear:both;">
<p>
<img alt="${name}" src="${image}" width="150px" class="" />
<strong>$name</strong>#if ($title != "")<strong>, $title </strong>#end
<br />
<strong>Contact:</strong><br />
#if ($phone != "")
Phone: $phone <br />
#end
#if ($office != "")
Office: $office <br />
#end
#if ($building != "")
Building: $building <br />
#end
#if ($email != "")
Email: <a href="mailto:${email}">$_EscapeTool.xml($email)</a> <br />
#end
#if ($website != "")
Profile: <a href="${website}">$_EscapeTool.xml($name)</a><br />
#end
<br />
</p>
</div>
#end
<hr />
#end
<br />
#end
#end
Discussions are closed to public comments.
If you need help with Cascade CMS please
start a new discussion.
Keyboard shortcuts
Generic
? | Show this help |
---|---|
ESC | Blurs the current field |
Comment Form
r | Focus the comment reply box |
---|---|
^ + ↩ | Submit the comment |
You can use Command ⌘
instead of Control ^
on Mac
1 Posted by Ryan Griffith on 15 May, 2015 01:33 PM
Hi Lori,
If you are trying to further sort your sections, you could add additional sorting criterion for the Sort Tool to use when sorting. To do so, simply add additional sort criterion in the desired order. For example:
If you need to sort the pages within the sections, you can use the Sort Tool again within the
#foreach
loop that is outputting the section. For example, let's say you want to sort the pages in each section by their building:Also, I wanted to note that the
#foreach
you have to build the$sectionArr
variable may not be necessary. Instead, I believe you can tweak the XPath used to obtain those dynamic metadata values to only get unique values. If you are interested, please attach sample XML that is being applied to this Format and I'd be happy to help. To obtain the sample XML, edit the Format and select the appropriate Block and context Page within the Preview Options section at the top.Please let me know if you have any questions.
Thanks!
2 Posted by lori on 25 May, 2015 07:21 PM
Hi Ryan,
I haven't quite got this figured out. Can you have a peak at my code and sample XML?
What I'm wanting to do is display all the pages that have the dynamic-metadata value "chair", then "assistant", then "faculty". Does that make sense?
Thanks for your help!
3 Posted by Ryan Griffith on 27 May, 2015 01:41 PM
Hi Lori,
Just to confirm, you would like to list these pages by position in that specific order? If that is the case, I don't think sorting would work since we're not really looking at an alphabetical order. Instead, I think you would be better off manually outputting the faculty and staff for each position type, so which would give you the most flexibility on order.
Here is a sample Format I was working on that should accomplish this:
Note: if you ever need to add/change the positions being displayed, simply modify the lines containing
#outputPosition(...)
to reflect the value of the position you wish to output.Please let me know if you have any questions.
Thanks!
4 Posted by lori on 27 May, 2015 01:46 PM
I will try this! Thank you :)
5 Posted by Ryan Griffith on 27 May, 2015 01:58 PM
Not a problem at all, Lori. Please keep me posted on how the Format works out, or if you have any questions about the changes.
Thanks!
Support Staff 6 Posted by Tim on 17 Jun, 2015 02:22 PM
Hi Lori,
Any luck with Ryan's recommendations? Let us know how things are going.
Thanks!
7 Posted by lori on 17 Jun, 2015 02:24 PM
Thanks for checking in. The recommendation made complete sense, but didn't quite render the results we're looking for. I'll post what I'm seeing a bit later today.
8 Posted by lori on 17 Jun, 2015 09:57 PM
So, with Ryan's recommendation, the output I'm getting is 3 loops (2 titled: ${position.get("positionName")} and one titled correctly "Faculty").. and all loops list all pages. This seems like the answer.. but something isn't working quite right.
9 Posted by Ryan Griffith on 18 Jun, 2015 12:25 PM
Thank you for the additional information, Lori.
Based on what you are seeing, it sounds as though the code that builds out the lookup table isn't able to find the name of the position.
When you have a moment, try using the following adjustment which will output the value of the position while building the lookup table so we can see what is being returned:
Please let me know if you have any questions.
Thanks!
10 Posted by lori on 18 Jun, 2015 02:29 PM
Ok, I see what's going on now. It is working. The issue came when there were no pages associated with one of the position values we're looking up. I wrapped an 'if' statement around the outputs and it works perfectly! Thanks for all your help on this!
The final code:
11 Posted by Ryan Griffith on 18 Jun, 2015 02:37 PM
Thank you for following up, Lori, I am glad to hear you were able to work through the issue and get things working.
I'm going to go ahead and close this discussion, please feel free to comment or reply to re-open if you have any additional questions.
Have a great day!
Ryan Griffith closed this discussion on 18 Jun, 2015 02:37 PM.