multi-select foreach

Nando's Avatar

Nando

29 Apr, 2016 01:10 PM

Here's my code and it works fine except when a staff member has 2 dept values - it only uses 1...

#set ($pages = $_XPathTool.selectNodes($contentRoot, "/system-index-block/system-page"))

#set ($depts = ['Application Support and Operations','Audio Visual and Video Conferencing','Business and Academic Solutions','Business Operations','Client Support Services','Clinical Informatics','Digital Media Production','Enterprise Systems and Infrastructure Services','Healthcare Security','Information Management','Information Security','Information Technology Support','Infrastructure and Collaboration Services','Innovation Zone (IZone)','Network Services','OIIT Leadership','Project Management','Software Development and Integration','Strategic Support Services','Technology Training','Telecommunications','Visual Interaction Design','Web Development'])

#foreach ($dept in $depts)
$dept
#foreach ($page in $pages)
#set ($dname = $_XPathTool.selectSingleNode($page, "dynamic-metadata[name = 'department']/value"))
#set ($path = $page.getChild("path").value)
##
#if($dname.value == $dept)
#if(!$path.contains("base-assets"))
$page.getChild("name").value
#end
#end
#end
#end

How do I have the staff member show up under every dept he's a part of?

thanks as always!!

  1. 1 Posted by Ryan Griffith on 29 Apr, 2016 08:27 PM

    Ryan Griffith's Avatar

    Hi Nando,

    What you can do is tweak your code slightly so you can use XPath to filter all pages that have a department that matches the current one within the loop. When you have a moment, give the following a try for starters:

    #set ($pages = $_XPathTool.selectNodes($contentRoot, "/system-index-block/system-page"))
    
    #set ($depts = ['Application Support and Operations','Audio Visual and Video Conferencing','Business and Academic Solutions','Business Operations','Client Support Services','Clinical Informatics','Digital Media Production','Enterprise Systems and Infrastructure Services','Healthcare Security','Information Management','Information Security','Information Technology Support','Infrastructure and Collaboration Services','Innovation Zone (IZone)','Network Services','OIIT Leadership','Project Management','Software Development and Integration','Strategic Support Services','Technology Training','Telecommunications','Visual Interaction Design','Web Development'])
    
    #foreach ($dept in $depts)
      $dept
      #set ($pagesInDept = $_XPathTool.selectSingleNode($pages, "dynamic-metadata[name = 'department']/value[. = '${dept}']"))
      #if ($pagesInDept.size() > 0)
        #foreach ($page in $pagesInDept)
          #set ($path = $page.getChild("path").value)
          #if(!$path.contains("base-assets"))
            $page.getChild("name").value
          #end
        #end
      #end
    #end
    

    Please let me know if you have any questions.

    Thanks!

  2. 2 Posted by Nando on 29 Apr, 2016 08:51 PM

    Nando's Avatar

    That just gave me all the Departments...

    The code above was used when i was hiding/showing full departments - I moved to hiding/showing staff members based on their dept now (sorry!).

    but I would still like to know how to do it bc I'll need it for something else...

    thanks again!

  3. 3 Posted by Ryan Griffith on 29 Apr, 2016 08:54 PM

    Ryan Griffith's Avatar

    By apologies, Nando, there was a typo in my proposed code. When you have a moment, please adjust the following line:

    
    

    to:

    #set ($pagesInDept = $_XPathTool.selectNodes($pages, ".[dynamic-metadata[name = 'department' and value = '${dept}']]"))
    

    Please let me know if you have any questions.

    Thanks!

  4. 4 Posted by Nando on 29 Apr, 2016 09:01 PM

    Nando's Avatar

    thats beautiful :) thanks!!

    regarding my changes, would you mind having a look and giving me your thoughts

    #set ($pages = $_XPathTool.selectNodes($contentRoot, "/system-index-block/system-page"))
    #set ($depts = ['Application Support and Operations','Audio Visual and Video Conferencing','Business and Academic Solutions','Business Operations','Clinical Informatics','Digital Media Production','Enterprise Systems and Infrastructure Services','Healthcare Security','Information Management','Information Security','Information Technology Support','Infrastructure and Collaboration Services','Innovation Zone (IZone)','OIIT Leadership','Network Services','Project Management','Software Development and Integration','Telecommunications','Visual Interaction Design','Web Development'])
    ##
    #set ($all = $depts.size())
    ##$all
    #set ($half = $all / 2)
    ##$half
    #if (($half % 1) < .5)
    #set ($num = ($half / 1) - ($half % 1))
    #else
    #set ($num = ($half / 1) - ($half % 1) + 1)   
    #end
    ##
    <div class="trigger defaultopen" id="dentalfilter"><a href="javascript:void(0);">Filter Results</a></div>
    <div class="toggle_container" style="display: block;">
    <div class="block">
    <p><strong>Leadership:</strong></p>
    <p><label for="filter_oiitleadership"><input checked="checked" id="filter_oiitleadership" name="filter" onchange="javascript:oiitfacultyFilter(this.value);" tabindex="1" type="radio" value="oiitleadership" /> OIIT Leadership</label></p>
    <div>
    <p><strong>Departments:</strong></p>
    <div style="float:left;width:auto;margin-right:10px;">
    #foreach ($dept in $depts)
    #if($dept != "OIIT Leadership")
    #set ($count = $foreach.index + 2)
    #set ($deptname = $dept.replaceAll("/[^a-zA-Z0-9]/","").replaceAll(" ","").replaceAll("[(]","").replaceAll("[)]","").replaceAll("[.]","").toLowerCase())
    #if($count == $num + 2)
    </div>
    <div style="float:left;width:auto;">
    #end
    <label for="filter_$deptname"><input id="filter_$deptname" name="filter" onchange="javascript:oiitfacultyFilter(this.value);" tabindex="$count" type="radio" value="$deptname" /> $dept.replaceAll("and","&amp;")</label><br />   
    #end
    #end
    </div>
    <br clear="all" />
    </div>
    <br clear="all" />
    </div>
    </div>
    <div id="filter_result" style="margin:10px 0px 20px 20px;">
    #foreach ($dept in $depts)
    #set ($deptname = $dept.replaceAll("/[^a-zA-Z0-9]/","").replaceAll(" ","").replaceAll("[(]","").replaceAll("[)]","").replaceAll("[.]","").toLowerCase())
    <h2 class="$deptname" style="margin-left:0px;display:none;">$dept</h2>
    #end
    #foreach ($page in $pages)
    #set ($path = $page.getChild("path").value)
    #set ($data = $page.getChild("system-data-structure"))
    #set ($fname = $data.getChild("first-name").value)
    #set ($depts = $_XPathTool.selectNodes($page, "dynamic-metadata[name = 'department']/value"))
    ##
    #if(!$path.contains("cascade"))
    <div class="$path card#foreach ($dept in $depts) $dept.value.replaceAll("/[^a-zA-Z0-9]/","").replaceAll(" ","").replaceAll("[(]","").replaceAll("[)]","").replaceAll("[.]","").toLowerCase()#end" style="float:left;min-height:145px;display:none;">
    $fname
    </div>
    #end
    #end
    </div>
    
  5. 5 Posted by Ryan Griffith on 02 May, 2016 12:54 PM

    Ryan Griffith's Avatar

    Hi Nando,

    In general, it looks like you are on the right track. I made a few tweaks to your format:

    #set ($pages = $_XPathTool.selectNodes($contentRoot, "/system-index-block/system-page"))
    #set ($depts = ['Application Support and Operations','Audio Visual and Video Conferencing','Business and Academic Solutions','Business Operations','Clinical Informatics','Digital Media Production','Enterprise Systems and Infrastructure Services','Healthcare Security','Information Management','Information Security','Information Technology Support','Infrastructure and Collaboration Services','Innovation Zone (IZone)','OIIT Leadership','Network Services','Project Management','Software Development and Integration','Telecommunications','Visual Interaction Design','Web Development'])
    #macro (sanitizeDeptName $deptName)
      $deptName.replaceAll("/[^a-zA-Z0-9]/","").replaceAll(" ","").replaceAll("[(]","").replaceAll("[)]","").replaceAll("[.]","").toLowerCase()
    #end
    ##
    #set ($all = $depts.size())
    ##$all
    #set ($half = $all / 2)
    ##$half
    #if (($half % 1) < .5)
      #set ($num = $half - ($half % 1))
    #else
      #set ($num = $half - ($half % 1) + 1)   
    #end
    ##
    <div class="trigger defaultopen" id="dentalfilter"><a href="javascript:void(0);">Filter Results</a></div>
    <div class="toggle_container" style="display: block;">
      <div class="block">
        <p><strong>Leadership:</strong></p>
        <p><label for="filter_oiitleadership"><input checked="checked" id="filter_oiitleadership" name="filter" onchange="javascript:oiitfacultyFilter(this.value);" tabindex="1" type="radio" value="oiitleadership" /> OIIT Leadership</label></p>
        <div>
          <p><strong>Departments:</strong></p>
          <div style="float:left;width:auto;margin-right:10px;">
          #foreach ($dept in $depts)
            #if($dept != "OIIT Leadership")
              #set ($count = $foreach.index + 2)
              #set ($deptname = #sanitizeDeptName($dept))
              #if($count == $num + 2)
                </div>
                <div style="float:left;width:auto;">
              #end
              <label for="filter_${deptname.trim()}"><input id="filter_${deptname.trim()}" name="filter" onchange="javascript:oiitfacultyFilter(this.value);" tabindex="$count" type="radio" value="$deptname" /> $dept.replaceAll("and","&amp;")</label><br />   
            #end
          #end
          </div>
          <br clear="all" />
        </div>
        <br clear="all" />
      </div>
    </div>
    <div id="filter_result" style="margin:10px 0px 20px 20px;">
    #foreach ($dept in $depts)
      #set ($pagesInDept = $_XPathTool.selectNodes($pages, ".[dynamic-metadata[name = 'department' and value = '${dept}']]"))
      #set ($deptname = "#sanitizeDeptName($dept)")
    
      <h2 class="${deptname.trim()}" style="margin-left:0px;display:none;">$dept</h2>
    
      #foreach ($page in $pagesInDept)
        #set ($path = $page.getChild("path").value)
        #set ($data = $page.getChild("system-data-structure"))
        #set ($fname = $data.getChild("first-name").value)
        #set ($depts = $_XPathTool.selectNodes($page, "dynamic-metadata[name = 'department']/value"))
        #set ($deptsClassName = "")
        #if ($depts.size() > 0)
          #foreach ($dept in $depts)
            #set ($deptsClassName = "${deptsClassName} #{sanitizeDeptName($dept.value)}")
          #end
        #end
        #if(!$path.contains("cascade"))
          <div class="$path card ${deptsClassName}" style="float:left;min-height:145px;display:none;">
            $fname
          </div>
        #end
      #end
    #end
    </div>
    

    Please let me know if you have any questions.

    Thanks!

  6. 6 Posted by Nando on 02 May, 2016 01:01 PM

    Nando's Avatar

    Def looks cleaner but I'm getting this error:

    An error occurred while rendering asset preview: Encountered "#sanitizeDeptName" at templateValidation[line 28, column 29]
    Was expecting one of:
        "[" ...
        "{" ...
        "(" ...
        
    <STRING_LITERAL> ...
        "true" ...
        "false" ...
        
        <INTEGER_LITERAL> ...
        
            <FLOATING_POINT_LITERAL> ...
        
                <IDENTIFIER> ...
        "{" ...
        "[" ...
    
  7. 7 Posted by Ryan Griffith on 02 May, 2016 01:10 PM

    Ryan Griffith's Avatar

    My apologies, Nando. When you have a moment, try changing line 28 to the following:

    #set ($deptname = "#sanitizeDeptName($dept)")
    

    Please let me know if you have any questions.

    Thanks!

  8. 8 Posted by Nando on 02 May, 2016 01:17 PM

    Nando's Avatar

    that was it, saw it after I posted - thanks!

    that macro is still giving me trouble in the divs below:

    #foreach ($dept in $depts)
            #set ($deptsClassName = "${deptsClassName} #{sanitizeDeptName($dept.value)}")
          #end
        #end
        #if(!$path.contains("cascade"))
          <div class="$path card ${deptsClassName}" style="float:left;min-height:145px;display:none;">
            $fname
          </div>
        #end
    

    the dept names aren't being "sanitized " - they're outputting like this:

    <div class="/oiit/video-conferencing/staff/data/clavier-gina card  #{sanitizeDeptName(Audio Visual and Video Conferencing)}" style="float:left;min-height:145px;display:none;">
            Gina
          </div>
    
  9. 9 Posted by Ryan Griffith on 02 May, 2016 01:29 PM

    Ryan Griffith's Avatar

    Thank you for following up, Nando.

    Let's try the following and let me know how it works out:

    #set ($deptsClassName = "${deptsClassName} #sanitizeDeptName($dept.value)")
    

    If that doesn't work, try replacing the single line with multiple:

    #set ($deptsClassName = "${deptsClassName} ")
    #set ($deptsClassName = "#sanitizeDeptName($dept.value)")
    

    Also, when outputting ${deptsClassName}, let's do ${deptsClassName.trim()} do remove extra whitespace.

    As a side note: you may want to rethinking outputting the path of the as a class name because I am pretty sure slashes are not valid for that attribute. Perhaps you can use a data- attribute, or replace all slashes with hyphens.

    Please let me know if you have any questions.

    Thanks!

  10. 10 Posted by Nando on 02 May, 2016 01:44 PM

    Nando's Avatar

    path was just there temp - took it out.

    this worked! thanks

    #set ($deptsClassName = "${deptsClassName} #sanitizeDeptName($dept.value)")
    

    Can you actually explain whats happening here ^^ I know the macro is sanitizing the dept name but how are you setting a variable with the same variable...

    Also, you were wrapping the staff divs in the dept foreach which we didnt need anymore plus that would just duplicate staff members but its so much cleaner now, thanks to you! this is my code now:

    #set ($pages = $_XPathTool.selectNodes($contentRoot, "/system-index-block/system-page"))
    #set ($depts = ['Application Support and Operations','Audio Visual and Video Conferencing','Business and Academic Solutions','Business Operations','Clinical Informatics','Digital Media Production','Enterprise Systems and Infrastructure Services','Healthcare Security','Information Management','Information Security','Information Technology Support','Infrastructure and Collaboration Services','Innovation Zone (IZone)','OIIT Leadership','Network Services','Project Management','Software Development and Integration','Telecommunications','Visual Interaction Design','Web Development'])
    #macro (sanitizeDeptName $deptName)
    $deptName.replaceAll("/[^a-zA-Z0-9]/","").replaceAll(" ","").replaceAll("[(]","").replaceAll("[)]","").replaceAll("[.]","").toLowerCase()##
    #end
    ##
    #set ($all = $depts.size())
    ##$all
    #set ($half = $all / 2)
    ##$half
    #if (($half % 1) < .5)
    #set ($num = $half - ($half % 1))
    #else
    #set ($num = $half - ($half % 1) + 1)   
    #end
    ##
    <div class="trigger defaultopen" id="dentalfilter"><a href="javascript:void(0);">Filter Results</a></div>
    <div class="toggle_container" style="display: block;">
    <div class="block">
    <p><strong>Leadership:</strong></p>
    <p><label for="filter_oiitleadership"><input checked="checked" id="filter_oiitleadership" name="filter" onchange="javascript:oiitfacultyFilter(this.value);" tabindex="1" type="radio" value="oiitleadership" /> OIIT Leadership</label></p>
    <div>
    <p><strong>Departments:</strong></p>
    <div style="float:left;width:auto;margin-right:10px;">
    #foreach ($dept in $depts)
    #if($dept != "OIIT Leadership")
    #set ($count = $foreach.index + 2)
    #set ($deptname = "#sanitizeDeptName($dept)")
    #if($count == $num + 2)
    </div>
    <div style="float:left;width:auto;">
    #end
    <label for="filter_${deptname.trim()}"><input id="filter_${deptname.trim()}" name="filter" onchange="javascript:oiitfacultyFilter(this.value);" tabindex="$count" type="radio" value="$deptname" /> $dept.replaceAll("and","&amp;")</label><br />   
    #end
    #end
    </div>
    <br clear="all" />
    </div>
    <br clear="all" />
    </div>
    </div>
    <div id="filter_result" style="margin:10px 0px 20px 20px;">
    #foreach ($dept in $depts)
    #set ($deptname = "#sanitizeDeptName($dept)")
    <h2 class="${deptname.trim()}" style="margin-left:0px;display:none;">$dept</h2>
    #end
    #foreach ($page in $pages)
    #set ($path = $page.getChild("path").value)
    #set ($data = $page.getChild("system-data-structure"))
    #set ($fname = $data.getChild("first-name").value)
    #set ($lname = $data.getChild("last-name").value)
    #set ($email = $data.getChild("email").value)
    #set ($creds = $data.getChild("credentials").value)
    #set ($phone = $data.getChild("phone").value)
    #set ($title = $data.getChild("title").value)
    #set ($image = $data.getChild("bio-image").getChild("path").value)
    #set ($depts = $_XPathTool.selectNodes($page, "dynamic-metadata[name = 'department']/value"))
    #set ($deptsClassName = "")
    #if ($depts.size() > 0)
    #foreach ($dept in $depts)
    #set ($deptsClassName = "${deptsClassName} #sanitizeDeptName($dept.value)")
    #end
    #end
    #if(!$path.contains("cascade"))
    <div class="card ${deptsClassName.trim()}" style="float:left;min-height:145px;display:none;">
    #if($image != "/")<img src="$image" border="0" alt="$fname.trim() $lname.trim()" class="polaroid right" width="75" style="margin: 10px 10px 10px 0px;" />#end
    <h4 style="font-weight:bold;">$fname.trim() $lname.trim()#if($creds != ""), $creds#end</h4>
    #if($title != "")<p style="margin-bottom:0px;"><strong>$_EscapeTool.xml($title).trim()</strong></p>#end##
    #if($phone != "")<p style="margin-bottom:0px;">$phone.trim()</p>#end##
    #if($email != "" && $email !="N/A")<p style="margin-bottom:0px;">$email.replaceAll("@nova.edu", "")##
    <!--#protect&#64;&#110;&#111;&#118;&#97;&#46;&#101;&#100;&#117;#protect--></p>#end##
    </div>
    #end
    #end
    </div>
    
  11. 11 Posted by Ryan Griffith on 02 May, 2016 03:37 PM

    Ryan Griffith's Avatar

    Thank you for following up, Nando. Glad to hear the latest change did the trick.

    Can you actually explain whats happening here ^^ I know the macro is sanitizing the dept name but how are you setting a variable with the same variable...

    We're just doing some simple Velocity string concatenation here, by setting the variable's value to it's current value PLUS the output of the macro.

    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!

  12. Ryan Griffith closed this discussion on 02 May, 2016 03:37 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