adding a conditional to a format for an index block

matthew.wren's Avatar

matthew.wren

Dec 10, 2015 @ 10:47 PM

Hello,

I recently just made a format that pulls content from an index block and everything is working fine but I'm trying to add one more bit of functionality to the page.

It basically just displays the content from a symlink a folder. I want it to add a "View Archives" link once the links exceed 10 in the folder.

here's the format


#set($mainTitle = $_XPathTool.selectSingleNode($contentRoot, '//system-data-structure/title').value)
<h1>$mainTitle</h1>
#set($links = $_XPathTool.selectNodes($contentRoot, '//system-index-block/system-symlink'))
#if ( $links.size() > 0 )
    #foreach ($l in $links)
        ## Link's Name
        #set ($title = $l.getChild('display-name').value)
       ## Date 
        #set ($start = $l.getChild('start-date').value)
        #set ( $date = $_DateTool.format("MMMM d, yyyy", $_DateTool.getDate($start) ) )
        ## Final
            <a href="$l.getChild('path').value"><p>${date} - $title</p></a>
    #end
#end
#if($links.size() > 10) <a href="/archive"><h3>View Archives</h3></a> #end <p>Follow us on our <a href="https://www.facebook.com/campusministryscranton">Facebook Page.</a></p>

As of now I just have the index block stop at 10 but I would rather have that in the format so it will only display 10 links and once that exceeds that much I want to add this link. How would I tweak this code to do that?

  1. 1 Posted by Ryan Griffith on Dec 11, 2015 @ 12:37 PM

    Ryan Griffith's Avatar

    Hi Matthew,

    What you can do is use a combination of the $foreach.index property available to you within loops along with the #break directive to end looping when you hit 10. Something like the following should do:

    #if ( $links.size() > 0 )
        #foreach ($l in $links)
            #if ($foreach.index > 9)
                #break
            #end
            
            ...
        #end
    #end
    

    Additionally, if all you need is to know if there are more than 10 pages, I would configure the Index Block to stop at 11. This helps avoid the Index Block becoming unnecessarily large. From there, all you need to do is check to see if the size is greater than 10 like you are already doing. One adjustment I would make, though, is to move that last #if statement inside of the first one that checks to see if there are any to begin with. So, with the changes from above, you would have something like:

    #if ( $links.size() > 0 )
        #foreach ($l in $links)
            #if ($foreach.index > 9)
                #break
            #end
            
            ...
        #end
        #if($links.size() > 10)
            <a href="/archive"><h3>View Archives</h3></a>
        #end
    #end
    <p>Follow us on our <a href="https://www.facebook.com/campusministryscranton">Facebook Page.</a></p>
    

    Please let me know if you have any questions.

    Thanks!

  2. 2 Posted by matthew.wren on Dec 11, 2015 @ 02:23 PM

    matthew.wren's Avatar

    Hey Ryan,

    I took the code you've provided and placed it within my format and I'm getting a really long error message. Did I misplace something?

    #set($mainTitle = $_XPathTool.selectSingleNode($contentRoot, '//system-data-structure/title').value)
    <h1>$mainTitle</h1>
    #set($links = $_XPathTool.selectNodes($contentRoot, '//system-index-block/system-symlink'))
    #if ( $links.size() > 0 )
        #foreach ($l in $links)
            #if ($foreach.index > 9)
                #set ($title = $l.getChild('display-name').value)   
                #set ($start = $l.getChild('start-date').value)
                #set ( $date = $_DateTool.format("MMMM d, yyyy", $_DateTool.getDate($start) ) )
                    <a href="$l.getChild('path').value"><p>${date} - $title</p></a>
            #break
        #end
    #end    
    
    #if($links.size() > 10)
        <a href="/archive"><h3>View Archives</h3></a>
    #end
    
    <a href="/archive"><h3>View Archives</h3></a>
    
    <p>Follow us on our <a href="https://www.facebook.com/campusministryscranton">Facebook Page.</a></p>
    
  3. 3 Posted by Ryan Griffith on Dec 11, 2015 @ 04:57 PM

    Ryan Griffith's Avatar

    Hi Matthew,

    A couple of things are jumping out:

    • You should place the #break within the #if that checks $foreach.index
    • You are missing the #end directive for the #if that checks $foreach.index
    • You are outputting your archives link twice

    Given the above, your Format would look more like:

    #set($mainTitle = $_XPathTool.selectSingleNode($contentRoot, '//system-data-structure/title').value)
    <h1>$mainTitle</h1>
    #set($links = $_XPathTool.selectNodes($contentRoot, '//system-index-block/system-symlink'))
    #if ( $links.size() > 0 )
        #foreach ($l in $links)
            #if ($foreach.index > 9)
                #break
            #end
    
            #set ($title = $l.getChild('display-name').value)   
            #set ($start = $l.getChild('start-date').value)
            #set ( $date = $_DateTool.format("MMMM d, yyyy", $_DateTool.getDate($start) ) )
            <a href="$l.getChild('path').value"><p>${date} - $title</p></a>
        #end
        #if($links.size() > 10)
            <a href="/archive"><h3>View Archives</h3></a>
        #end
    #end    
    
    <p>Follow us on our <a href="https://www.facebook.com/campusministryscranton">Facebook Page.</a></p>
    

    Please let me know if you have any questions.

    Thanks!

  4. 4 Posted by matthew.wren on Dec 15, 2015 @ 09:09 PM

    matthew.wren's Avatar

    that worked! thank you Ryan!

    Is there a way to add a sort tool to sort chronologically?

  5. 5 Posted by Ryan Griffith on Dec 16, 2015 @ 12:48 PM

    Ryan Griffith's Avatar

    Not a problem at all, Matthew, glad to hear the updated Format did the trick.

    Is there a way to add a sort tool to sort chronologically?

    Yes. Based on what I am seeing in the Format, it sounds like you would want to sort by the Start Date metadata field. This can be accomplished using the Sort Tool and adding a criterion. The relevant portion of your Format would look something like:

    #set($links = $_XPathTool.selectNodes($contentRoot, '//system-index-block/system-symlink'))
    #if ( $links.size() > 0 )
        $_SortTool.addSortCriterion("start-date", "", "number", "ascending", "upper-first")  
        $_SortTool.sort($links) 
        #foreach ($l in $links)
            ...
        #end
        ...
    #end
    

    Note: I wasn't sure which order you were looking for, so replace ascending with descending if you need the opposite order.

    Please let me know if you have any questions.

    Thanks!

  6. Tim closed this discussion on Jan 07, 2016 @ 04:32 PM.

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