tag:help-archives.hannonhill.com,2010-02-09:/discussions/velocity-formats/11949-custom-sorting-orderCascade CMS: Discussion 2015-06-18T14:37:25Ztag:help-archives.hannonhill.com,2010-02-09:Comment/368373792015-05-15T13:33:37Z2015-05-15T13:33:37ZCustom sorting order<div><p>Hi Lori,</p>
<p>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:</p>
<pre>
<code>$_SortTool.addSortCriterion( "value", "en", "text", "ascending", "upper-first")
$_SortTool.addSortCriterion( ... )
$_SortTool.addSortCriterion( ... )
$_SortTool.sort($sections)</code>
</pre>
<p>If you need to sort the pages within the sections, you can use
the Sort Tool again within the <code>#foreach</code> loop that is
outputting the section. For example, let's say you want to sort the
pages in each section by their building:</p>
<pre>
<code>## 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 )
$_SortTool.addSortCriterion( "system-data-structure/contactInfo/building", "en", "text", "ascending", "upper-first")
$_SortTool.sort($pages)
#foreach ( $page in $pages )
...
#end
#end
#end</code>
</pre>
<p>Also, I wanted to note that the <code>#foreach</code> you have
to build the <code>$sectionArr</code> 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 <em>Preview Options</em> section at the top.</p>
<p>Please let me know if you have any questions.</p>
<p>Thanks!</p></div>Ryan Griffithtag:help-archives.hannonhill.com,2010-02-09:Comment/368373792015-05-25T19:21:32Z2015-05-25T19:21:32ZCustom sorting order<div><p>Hi Ryan,<br>
I haven't quite got this figured out. Can you have a peak at my
code and sample XML?</p>
<pre>
<code>#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[name != 'index' ") )
$_SortTool.addSortCriterion("dynamic-metadata[value = 'chair']", "", "text", "descending", "")
$_SortTool.addSortCriterion("dynamic-metadata[value = 'assistant']", "", "text", "descending", "")
$_SortTool.addSortCriterion("dynamic-metadata[value = 'faculty']", "", "text", "descending", "")
$_SortTool.sort($sections)
#foreach ( $section in $sections )
#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</code>
</pre>
<p>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?<br>
Thanks for your help!</p></div>loritag:help-archives.hannonhill.com,2010-02-09:Comment/368373792015-05-27T13:41:02Z2015-05-27T13:41:02ZCustom sorting order<div><p>Hi Lori,</p>
<p>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.</p>
<p>Here is a sample Format I was working on that should accomplish
this:</p>
<pre>
<code>#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 />
## Get the unique positions and add generate a lookup table.
#set ( $lookup = {} )
#set( $positions = $_XPathTool.selectNodes($contentRoot, "//system-page[name != 'index' and not(dynamic-metadata[name = 'position']/value = preceding::system-page/dynamic-metadata[name = 'position']/value)]/dynamic-metadata[name = 'position']/value") )
#foreach ( $position in $positions )
#set($facstaff = $_XPathTool.selectNodes($contentRoot, "//system-page[name != 'index' and dynamic-metadata[name = 'position' and value = '${position.value}']]"))
$!lookup.put($position.value, {
"positionName": $position.value,
"facstaff": $facstaff
})
#end
## Output faculty and staff for each position.
#outputPosition($lookup.get("Chair"))
#outputPosition($lookup.get("Assistant"))
#outputPosition($lookup.get("Faculty"))
#macro (outputPosition $position)
#set ( $facstaff = $position.get("facstaff") )
<h2>${position.get("positionName")}</h2>
#if ( $facstaff.size() > 0 )
#foreach ( $page in $facstaff )
#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</code>
</pre>
<p>Note: if you ever need to add/change the positions being
displayed, simply modify the lines containing
<code>#outputPosition(...)</code> to reflect the value of the
position you wish to output.</p>
<p>Please let me know if you have any questions.</p>
<p>Thanks!</p></div>Ryan Griffithtag:help-archives.hannonhill.com,2010-02-09:Comment/368373792015-05-27T13:46:30Z2015-05-27T13:46:30ZCustom sorting order<div><p>I will try this! Thank you :)</p></div>loritag:help-archives.hannonhill.com,2010-02-09:Comment/368373792015-05-27T13:58:49Z2015-05-27T13:58:49ZCustom sorting order<div><p>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.</p>
<p>Thanks!</p></div>Ryan Griffithtag:help-archives.hannonhill.com,2010-02-09:Comment/368373792015-06-17T14:22:59Z2015-06-17T14:22:59ZCustom sorting order<div><p>Hi Lori,</p>
<p>Any luck with Ryan's recommendations? Let us know how things are
going.</p>
<p>Thanks!</p></div>Timtag:help-archives.hannonhill.com,2010-02-09:Comment/368373792015-06-17T14:24:43Z2015-06-17T14:24:43ZCustom sorting order<div><p>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.</p></div>loritag:help-archives.hannonhill.com,2010-02-09:Comment/368373792015-06-17T21:57:18Z2015-06-17T21:57:18ZCustom sorting order<div><p>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.</p></div>loritag:help-archives.hannonhill.com,2010-02-09:Comment/368373792015-06-18T12:25:54Z2015-06-18T12:25:54ZCustom sorting order<div><p>Thank you for the additional information, Lori.</p>
<p>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.</p>
<p>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:</p>
<pre>
<code>#set( $positions = $_XPathTool.selectNodes($contentRoot, "//system-page[name != 'index' and not(dynamic-metadata[name = 'position']/value = preceding::system-page/dynamic-metadata[name = 'position']/value)]/dynamic-metadata[name = 'position']/value") )
#foreach ( $position in $positions )
## Debugging position name
$position.value
#set($facstaff = $_XPathTool.selectNodes($contentRoot, "//system-page[name != 'index' and dynamic-metadata[name = 'position' and value = '${position.value}']]"))
$!lookup.put($position.value, {
"positionName": $position.value,
"facstaff": $facstaff
})
#end</code>
</pre>
<p>Please let me know if you have any questions.</p>
<p>Thanks!</p></div>Ryan Griffithtag:help-archives.hannonhill.com,2010-02-09:Comment/368373792015-06-18T14:29:57Z2015-06-18T14:29:57ZCustom sorting order<div><p>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!</p>
<p>The final code:</p>
<pre>
<code>#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 />
## Get the unique positions and add generate a lookup table.
#set ( $lookup = {} )
#set( $positions = $_XPathTool.selectNodes($contentRoot, "//system-page[name != 'index' and not(dynamic-metadata[name = 'position']/value = preceding::system-page/dynamic-metadata[name = 'position']/value)]/dynamic-metadata[name = 'position']/value") )
#foreach ( $position in $positions )
## Debugging position name
## $position.value
#set($facstaff = $_XPathTool.selectNodes($contentRoot, "//system-page[name != 'index' and dynamic-metadata[name = 'position' and value = '${position.value}']]"))
$!lookup.put($position.value, {
"positionName": $position.value,
"facstaff": $facstaff
})
#end
## Output faculty and staff for each position.
#if ($lookup.get("Chair") )
#outputPosition($lookup.get("Chair"))
#end
#if ($lookup.get("Assistant") )
#outputPosition($lookup.get("Assistant"))
#end
#if ($lookup.get("Faculty") )
#outputPosition($lookup.get("Faculty"))
#end
#if ($lookup.get("Contract") )
#outputPosition($lookup.get("Contract"))
#end
#if ($lookup.get("Sessional") )
#outputPosition($lookup.get("Sessional"))
#end
#if ($lookup.get("Student Researcher") )
#outputPosition($lookup.get("Student Researcher"))
#end
#if ($lookup.get("Senior Scholar") )
#outputPosition($lookup.get("Senior Scholar"))
#end
#macro (outputPosition $position)
#set ( $facstaff = $position.get("facstaff") )
<h2>${position.get("positionName")}</h2>
#if ( $facstaff.size() > 0 )
#foreach ( $page in $facstaff )
#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</code>
</pre></div>loritag:help-archives.hannonhill.com,2010-02-09:Comment/368373792015-06-18T14:37:24Z2015-06-18T14:37:24ZCustom sorting order<div><p>Thank you for following up, Lori, I am glad to hear you were
able to work through the issue and get things working.</p>
<p>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.</p>
<p>Have a great day!</p></div>Ryan Griffith