Indexing by first letter in title

steelej's Avatar

steelej

25 Oct, 2012 01:24 PM

I've been tasked with creating a site will all the institute's policies. One thing they want is a page listing all policies alphabetically. That's easy enough to index pages and order by title. What I'm trying to figure out is how to separate the titles out by first letter *(all titles that start with A, B, etc.) so I can insert anchors for navigation in the long list. Any suggestions?

  1. 1 Posted by Ryan Griffith on 25 Oct, 2012 03:07 PM

    Ryan Griffith's Avatar

    Hi,

    Just to confirm, you're looking to do this in Velocity, correct?

    If so, I believe one method you can do is:

    • Initialize an empty array #set($titleIndex = [])
    • Loop through the sorted index
    • Grab the first letter of the title and "normalize" it using String.charAt(0).toUpperCase()
    • Add this letter to the array from above using the Array.add() method
    • 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
    system-page[translate(substring(title, 1, 1), 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ') = '${index}']
    
    • Output the anchor/index letter along and loop through the resulting policies and output them

    Please let me know if you have any questions.

    Thanks

  2. 2 Posted by steelej on 25 Oct, 2012 04:31 PM

    steelej's Avatar

    Thanks, Ryan. I'll give this a go.

  3. 3 Posted by steelej on 25 Oct, 2012 06:55 PM

    steelej's Avatar

    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.

    Jim

  4. 4 Posted by Ryan Griffith on 25 Oct, 2012 08:14 PM

    Ryan Griffith's Avatar

    Hi Jim,

    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:

    #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
    

    Please let me know if you have any questions.

    Thanks

  5. 5 Posted by steelej on 25 Oct, 2012 08:21 PM

    steelej's Avatar

    It's great. Thank you. I'll spend time with this to get the logic and the syntax.

    As ever, I really appreciate the help.

    Jim

  6. steelej closed this discussion on 25 Oct, 2012 08:21 PM.

Comments are currently closed for this discussion. You can start a new one.

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