adding a conditional to a format for an index block
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?
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 Dec 11, 2015 @ 12:37 PM
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: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:Please let me know if you have any questions.
Thanks!
2 Posted by matthew.wren on Dec 11, 2015 @ 02:23 PM
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?
3 Posted by Ryan Griffith on Dec 11, 2015 @ 04:57 PM
Hi Matthew,
A couple of things are jumping out:
#break
within the#if
that checks$foreach.index
#end
directive for the#if
that checks$foreach.index
Given the above, your Format would look more like:
Please let me know if you have any questions.
Thanks!
4 Posted by matthew.wren on Dec 15, 2015 @ 09:09 PM
that worked! thank you Ryan!
Is there a way to add a sort tool to sort chronologically?
5 Posted by Ryan Griffith on Dec 16, 2015 @ 12:48 PM
Not a problem at all, Matthew, glad to hear the updated Format did the trick.
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:
Note: I wasn't sure which order you were looking for, so replace
ascending
withdescending
if you need the opposite order.Please let me know if you have any questions.
Thanks!
Tim closed this discussion on Jan 07, 2016 @ 04:32 PM.