Help w/ For Each

Graham Lewis's Avatar

Graham Lewis

04 Apr, 2014 03:58 PM

IS there a way to have a for each that looks through an index block of blocks and only shows ones that have a field marked a certain way? I am making a site index that has each listing in its own block with one of the fields a letter chooser to help organize out by the letter.

  1. 1 Posted by Ryan Griffith on 04 Apr, 2014 06:35 PM

    Ryan Griffith's Avatar

    Hi Graham,

    There are two ways you could go about doing this:

    • Use the XPath Tool to filter your elements by the field value. This way, you are only looping over the items that you want
    • Add an #if statement to check the value of your field, and if the condition is true do some code or output something

    Please let me know if you have any questions.

    Thanks!

  2. 2 Posted by Graham Lewis on 04 Apr, 2014 06:37 PM

    Graham Lewis's Avatar

    Thanks! I was thinking the #if - how would you do the tool filter for a field?

    Graham Lewis
    Webmaster
    Jacksonville State University

    256.782.5331
    www.jsu.edu

  3. 3 Posted by Ryan Griffith on 04 Apr, 2014 06:43 PM

    Ryan Griffith's Avatar

    Not a problem at all. You would use the XPathTool.selectNodes() function in conjunction with an XPath expression that checks the value of the field you want.

    If you could attach some sample XML from the Index Block and what field and value you want to filter by, I would be happy to help out with the XPath expression.

    Please let me know if you have any questions.

    Thanks!

  4. 4 Posted by Graham Lewis on 04 Apr, 2014 06:45 PM

    Graham Lewis's Avatar

    Sure - here it is.

    <system-block id="2db3b9797f000001036762711cbc28f1"> 
    <name>Academic and Student Affairs</name> 
    <path>/listings/Academic and Student Affairs</path> 
    <site>_Sitemap</site> 
    <system-data-structure definition-path="Listing Builder"> 
    <name>Academic &amp; Student Affairs</name> 
    <letter>a</letter> 
    <category> 
    <value>Academic</value> 
    <value>Student</value> 
    </category> 
    <link-chooser /> 
    <page type="page"> 
    <content /> 
    <path>/index</path> 
    <dynamic-metadata> 
    <name>navigation</name> 
    <value>No</value> 
    </dynamic-metadata> 
    <link>site://Academic Affairs/index</link> 
    <site>Academic Affairs</site> 
    <name>index</name> 
    <display-name>Academic Affairs</display-name> 
    <title>Welcome</title> 
    </page> 
    <url /> 
    </system-data-structure>
    

    Graham Lewis
    Webmaster
    Jacksonville State University

    256.782.5331
    www.jsu.edu

  5. 5 Posted by Ryan Griffith on 04 Apr, 2014 06:54 PM

    Ryan Griffith's Avatar

    Thank you for providing the XML, Graham. So let's say you want to filter system-blocks by the letter a:

    #set ($blocks = $_XPathTool.selectNodes($contentRoot, "//system-block[system-data-structure/letter = 'a']"))
    #if ($blocks.size() > 0)
        ... Do something with blocks ...
    #end
    

    Or, let's say you want the letter to be dynamic based on a field on your page called letter:

    #set ($letter = $_XPathTool.selectSingleNode($contentRoot, "//calling-page/system-page/system-data-structure/letter"))
    #set ($blocks = $_XPathTool.selectNodes($contentRoot, "//system-block[system-data-structure/letter = '${letter}']"))
    #if ($blocks.size() > 0)
        ... Do something with blocks ...
    #end
    

    Please let me know if you have any questions.

    Thanks!

  6. 6 Posted by Graham Lewis on 04 Apr, 2014 06:57 PM

    Graham Lewis's Avatar

    Ah - that makes sense. That's gonna be handy! Thanks

    Graham Lewis
    Webmaster
    Jacksonville State University

    256.782.5331
    www.jsu.edu

  7. 7 Posted by Ryan Griffith on 04 Apr, 2014 07:13 PM

    Ryan Griffith's Avatar

    Not a problem at all, Graham. I am glad to hear the snippets helped out.

    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.

    Have a great day!

  8. Ryan Griffith closed this discussion on 04 Apr, 2014 07:13 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

 

26 Aug, 2016 01:19 PM
25 Aug, 2016 03:02 PM
25 Aug, 2016 12:50 PM
24 Aug, 2016 08:43 PM
24 Aug, 2016 07:20 PM
21 Aug, 2016 01:20 PM