Looping parent element not showing

zepedac's Avatar

zepedac

28 Jan, 2016 11:37 PM

I can't figure out how to loop the section. Each has a section that has a title that is also not looping. How can I make each loop and include respectively for each? Basically how the XML is structured, it's how I want the output to be as well. Hope that makes sense?

XML: Below is the short version

<?xml version="1.0" encoding="UTF-8"?>
<system-data-structure>
    <main-set>
        <plain-content>
            <h3>Main Set One</h3>
        </plain-content>
        <set>...</set>
    </main-set>
    <main-set>
        <plain-content>
            <h3>Main Set Two</h3>
        </plain-content>
        <set>...</set>
    </main-set>
    <main-set>
        <plain-content>
            <h3>Main Set Three</h3>
        </plain-content>
        <set>...</set>
    </main-set>
</system-data-structure>

Velocity: Full

#set($main_set = $_XPathTool.selectNodes($contentRoot, "//main-set"))
#set($wysiwyg = $_XPathTool.selectNodes($contentRoot, "//plain-content"))
#set($accordions = $_XPathTool.selectNodes($contentRoot, "//set"))

#if ($main_set.size() > 0)
    
    #set($plain_content = $_XPathTool.selectSingleNode($wysiwyg, "//plain-content"))

    ## HTML output.
    <div class="main-set">
    ${_SerializerTool.serialize($plain_content, true)}
    
        <ul id="faqs">
        #if ($accordions.size() > 0)
              #foreach($accordion in $accordions)
                  #set($section_title = $_XPathTool.selectSingleNode($accordion, "section-title").value)
                  #set($content = $_XPathTool.selectSingleNode($accordion, "content"))
                  ## HTML output.
                      <li class="faq-set">
                          <p class="question">${_EscapeTool.xml($section_title)}</p>
                          <div class="answer">${_SerializerTool.serialize($content, true)}</div>
                      </li>
              #end
        #end
        </ul>
        
    </div>    
#else
    ## Don't display $plain_content
#end
  1. 1 Posted by Ryan Griffith on 29 Jan, 2016 02:54 PM

    Ryan Griffith's Avatar

    Hi,

    It looks like you're going to want to tweak this a bit. Specifically, you want to loop over each main-set and output the title and accordion within them, not loop over things individually.

    Here is a sample Format to help get you started:

    #set($main_sets = $_XPathTool.selectNodes($contentRoot, "//main-set"))
    #set($wysiwyg = $_XPathTool.selectNodes($contentRoot, "//plain-content"))
    #set($accordions = $_XPathTool.selectNodes($contentRoot, "//set"))
    
    #if ($main_set.size() > 0)
      #foreach ($main_set in $main_set)
        #set($accordions = $_XPathTool.selectNodes($main_set, "set"))
        #set($wysiwyg = $main_set.getChild("plain-content"))
      
        ## HTML output.
        <div class="main-set">
          ${_SerializerTool.serialize($wysiwyg, true)}
        
          #if ($accordions.size() > 0)
            <ul id="faqs">
              #foreach($accordion in $accordions)
                #set($section_title = $_XPathTool.selectSingleNode($accordion, "section-title").value)
                #set($content = $_XPathTool.selectSingleNode($accordion, "content"))
                ## HTML output.
                <li class="faq-set">
                  <p class="question">${_EscapeTool.xml($section_title)}</p>
                  <div class="answer">${_SerializerTool.serialize($content, true)}</div>
                </li>
              #end
            </ul>
          #end  
        </div>   
      #end
    #end
    

    Please let me know if you have any questions.

    Thanks!

  2. 2 Posted by Wing Ming Chan on 29 Jan, 2016 03:06 PM

    Wing Ming Chan's Avatar

    Hi,

    I don't how how the ul's and li's are supposed to be nested. But I've come up with this, after cleaning up the code a bit:

    #set( $main_sets = $contentRoot.getChildren( "main-set" ) )
    
    #if( $main_sets.size() > 0 )
    <div class="main-set">
        #foreach( $main_set in $main_sets )
        <ul id="faqs">
            #set( $plain_content = $main_set.getChild( "plain-content" ) )
            $_SerializerTool.serialize( $plain_content, true )
            #set( $sets = $main_set.getChildren( "set" ) )
            
            #if( $sets.size() > 0 )
                #foreach( $set in $sets )
                    #set( $section_title = $set.getChild( "section-title" ).value )
                    #set( $content       = $set.getChild( "content" ) )
                    <li class="faq-set">
                    <p class="question">$_EscapeTool.xml( $section_title )</p>
                    <div class="answer">$_SerializerTool.serialize( $content, true )</div>
                    </li>
                #end
            #end
        </ul>
        #end
    </div>    
    #end
    

    Wing

  3. 3 Posted by zepedac on 01 Feb, 2016 09:38 PM

    zepedac's Avatar

    I'm working on these two suggestions. I'll update y'all with my results.

  4. 4 Posted by Ryan Griffith on 01 Feb, 2016 09:56 PM

    Ryan Griffith's Avatar

    Thank you for following up. Please keep us posted on your progress.

    Have a great day!

  5. Ryan Griffith closed this discussion on 11 Feb, 2016 01:38 PM.

  6. zepedac re-opened this discussion on 06 Mar, 2016 04:22 AM

  7. 5 Posted by zepedac on 06 Mar, 2016 04:22 AM

    zepedac's Avatar

    I finally figured it out. A few things. Syntax misspelling with $main_sets, $wysiwygs and also creating a counter to increment by one for the faqs id. I also removed the forward slashes when setting the nodes for <main-set />, <plain-content />, and <set />. This is improving upon the code that Ryan provided.

    ## Removed the foward slashes //.
    ## Renamed $wysiwyg to $wysiwygs with an "s".
    #set($main_sets = $_XPathTool.selectNodes($contentRoot, "main-set"))
    #set($wysiwygs = $_XPathTool.selectNodes($contentRoot, "plain-content"))
    #set($accordions = $_XPathTool.selectNodes($contentRoot, "set"))
    ## Add a count for incrementing the "id='faqs-#'.
    #set ( $count = 0 )
    
    #if ($main_sets.size() > 0)
      #foreach ($main_set in $main_sets)
        #set($accordions = $_XPathTool.selectNodes($main_set, "set"))
        #set($wysiwyg = $main_set.getChild("plain-content"))
        ## Increment the $count variable by 1.
        #set ($count = $count + 1)
          
        ## HTML output.
        <div class="main-set">
          ${_SerializerTool.serialize($wysiwyg, true)}
        
          #if ($accordions.size() > 0)
          ## Adding the $count variable in order to increment the "id" in order to have more than one "id".
            <ul id="faqs-$count">
              #foreach($accordion in $accordions)
                #set($section_title = $_XPathTool.selectSingleNode($accordion, "section-title").value)
                #set($content = $_XPathTool.selectSingleNode($accordion, "content"))
                ## HTML output.
                <li class="faq-set">
                  <p class="question">${_EscapeTool.xml($section_title)}</p>
                  <div class="answer">${_SerializerTool.serialize($content, true)}</div>
                </li>
              #end
            </ul>
            <br /><br />
          #end  
        </div>   
      #end
    #end
    
  8. 6 Posted by Ryan Griffith on 07 Mar, 2016 01:20 PM

    Ryan Griffith's Avatar

    Thank you for following up. I am glad to hear you were able to use our sample code to develop a working solution.

    and also creating a counter to increment by one for the faqs id

    I wanted to mention that each loop is given a $foreach context variable which has an index property that accomplishes this for you. You simply use $foreach.index.

    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!

  9. Ryan Griffith closed this discussion on 07 Mar, 2016 01:20 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