tag:help-archives.hannonhill.com,2010-02-09:/discussions/velocity-formats/276-indexing-by-first-letter-in-titleCascade CMS: Discussion 2018-10-18T20:36:41Ztag:help-archives.hannonhill.com,2010-02-09:Comment/200319132012-10-25T15:07:05Z2012-10-25T15:07:05ZIndexing by first letter in title<div><p>Hi,</p>
<p>Just to confirm, you're looking to do this in Velocity,
correct?</p>
<p>If so, I believe one method you can do is:</p>
<ul>
<li>Initialize an empty array <code>#set($titleIndex =
[])</code></li>
<li>Loop through the sorted index</li>
<li>Grab the first letter of the title and "normalize" it using
<code>String.charAt(0).toUpperCase()</code></li>
<li>Add this letter to the array from above using the
<code>Array.add()</code> method</li>
<li>Loop through this array and either use Xpath to grab only
policies with a title that start with the given index. Something
like the following should work. Note you have to use translate to
convert the first letter of the title</li>
</ul>
<pre>
<code>system-page[translate(substring(title, 1, 1), 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ') = '${index}']</code>
</pre>
<ul>
<li>Output the anchor/index letter along and loop through the
resulting policies and output them</li>
</ul>
<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/200319132012-10-25T16:31:34Z2012-10-25T16:31:34ZIndexing by first letter in title<div><p>Thanks, Ryan. I'll give this a go.</p></div>steelejtag:help-archives.hannonhill.com,2010-02-09:Comment/200319132012-10-25T18:55:20Z2012-10-25T18:55:20ZIndexing by first letter in title<div><p>Ryan, I've been wrangling with this with little luck as yet. I'm
not sure of the syntax for these strings. If you can give me a
push, I'd appreciate it. I've attached the block, and a simple
format that is outputting the text and path in order.</p>
<p>Jim</p></div>steelejtag:help-archives.hannonhill.com,2010-02-09:Comment/200319132012-10-25T20:14:10Z2012-10-25T20:14:10ZIndexing by first letter in title<div><p>Hi Jim,</p>
<p>Looks like you're on the right track, but you'll want to
surround your loop with another one that basically loops through
the alphabet. The following should get you started:</p>
<pre>
<code>#set ($alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
#set ($alphabetArr = $alphabet.toCharArray())
#foreach ($letter in $alphabetArr)
#set ($assets = $_XPathTool.selectNodes($contentRoot, "//system-page[system-data-structure/title[starts-with(translate(., 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'), '${letter}')]]"))
#if ($assets.size() > 0)
<a href="#${letter}"></a>
<h3>${letter}</h3>
$_SortTool.addSortCriterion("system-data-structure/title", "", "text", "ascending", "upper-first")
$_SortTool.sort($assets)
<ul>
#foreach ($asset in $assets)
#set ( $pagename = $_XPathTool.selectSingleNode($asset, "system-data-structure/title").value)
#set ($path = $_XPathTool.selectSingleNode($asset, "path").value)
<li><a href="${path}">${pagename}</a></li>
#end
</ul>
#end
#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/200319132012-10-25T20:21:27Z2012-10-25T20:21:27ZIndexing by first letter in title<div><p>It's great. Thank you. I'll spend time with this to get the
logic and the syntax.</p>
<p>As ever, I really appreciate the help.</p>
<p>Jim</p></div>steelej