macro to create alpha listing

lauren.fraser's Avatar

lauren.fraser

09 Jul, 2015 05:47 PM

I'm working on a page similar to this: http://www.pennmedicine.org/health-system/patient/departments-servi...

I have this format, that works, but I'm wondering if there is a way to use a macro to loop through the alphabet and create the $isA, $isB, etc variables, rather than writing the code for each letter.

#set ($isA = $_XPathTool.selectNodes($contentRoot, "/system-index-block/system-page[dynamic-metadata[(name='letter' or name='second-letter') and value ='A']]") )
#if ($isA.size() > 0) #set ($hasA = true) #else #set ($hasA = false) #end #if ($hasA)
<h2>A</h2> #foreach ($a in $isA) #set ($specTitle = $a.getChild("display-name")) #set ($specPath = $a.getChild("link").value) <p><a href="${specPath}"> $_EscapeTool.xml($specTitle.value)</a></p> #end #end


#set ($isB = $_XPathTool.selectNodes($contentRoot, "/system-index-block/system-page[dynamic-metadata[(name='letter' or name='second-letter') and value ='B']]") )
#if ($isB.size() > 0) #set ($hasB = true) #else #set ($hasB = false) #end #if ($hasB)
<h2>B</h2> #foreach ($b in $isB) #set ($specTitle = $b.getChild("display-name")) #set ($specPath = $b.getChild("link").value) <p><a href="${specPath}"> $_EscapeTool.xml($specTitle.value)</a></p> #end #end
  1. 1 Posted by Wing Ming Chan on 10 Jul, 2015 06:20 PM

    Wing Ming Chan's Avatar

    Hi,

    Because I do not have your index block to test the code, I can only try my best. You may need to make minor adjustments here and there. But here it is:

    #macro( repeatAlpha $alpha )
        #set( $isVar = 
            $_XPathTool.selectNodes( 
                $contentRoot, 
                "/system-index-block/system-page[dynamic-metadata[(name='letter' or name='second-letter') and value='$alpha']]" ) )
        #if( $isVar.size() > 0 )
            <h2>$alpha</h2>
            #foreach ( $a in $isVar )
                #set ( $specTitle = $a.getChild( "display-name" ) )
                #set ( $specPath = $a.getChild( "link" ).value )
                <p><a href="${specPath}"> $_EscapeTool.xml( $specTitle.value )</a></p>
            #end
        #end
    #end
    
    #set( $alphabets = [ 'A', 'B', 'C', 'D', 'E', 'F' ] )
    
    #foreach( $al in $alphabets )
        #repeatAlpha( $al )
    #end
    

    Good luck!

    Wing

  2. 2 Posted by lauren.fraser on 10 Jul, 2015 07:28 PM

    lauren.fraser's Avatar

    Thanks Wing! We’re currently upgrading our instance Cascade, so I can’t test. When I set $alphabets, will I need to continue for all the letters in the alphabet?

    I know it will save me time on the coding end, but will the macro render faster than the long version I had started (just curious).

  3. 3 Posted by Wing Ming Chan on 10 Jul, 2015 07:39 PM

    Wing Ming Chan's Avatar

    Yes, you'll need to add more to $alphabets. As far as rendering is concerned, I don't think there will be any significant difference. But you had better check with the people in Hannon Hill.

    Wing

  4. 4 Posted by Ryan Griffith on 13 Jul, 2015 01:25 PM

    Ryan Griffith's Avatar

    Hi Lauren,

    I suspect the rendering will be the same either way, because in the end you are still doing the same number of XPath Tool calls and #if tests. The only difference is the #foreach that loops over the alphabet array, which should be negligible.

    That being said, if performance is a concern, you can certainly try both ways and check the page's rendering metrics to compare the region's rendering time with both versions.

    In my opinion, even if there is is a difference, Wing's example is much easier to read and maintain going forward.

    Please let me know if you have any questions.

    Thanks!

  5. 5 Posted by lauren.fraser on 13 Jul, 2015 06:11 PM

    lauren.fraser's Avatar

    Wing,
    I get the <br /> tags output, but not any content. Here is a sample from my index block:

    system-index-block name="specialty-index" type="content_type" current-time="1436811018665">
        <system-page id="2164e5a4ac1009d000cf095e31fa50d6">
            <name>index</name>
            <is-published>true</is-published>
            <last-published-on>1436808074151</last-published-on>
            <last-published-by>zung</last-published-by>
            <title>Ear, Nose &amp; Throat</title>
            <created-by>zung</created-by>
            <created-on>1435079337365</created-on>
            <last-modified-by>lfraser</last-modified-by>
            <last-modified>1436807356978</last-modified>
            <dynamic-metadata>
                <name>letter</name>
                <value>E</value>
            </dynamic-metadata>
            <dynamic-metadata>
                <name>first-spec</name>
                <value>Ear, Nose &amp; Throat</value>
            </dynamic-metadata>
            <dynamic-metadata>
                <name>second-spec</name>
                <value>O</value>
            </dynamic-metadata>
            <dynamic-metadata>
                <name>second-letter</name>
                <value>Otolaryngology</value>
            </dynamic-metadata>
        </system-page>
    

    If I remove the macro and change the $isVar to this, it will output all of the A's, so I'm not sure what to change.

    #set( $isVar = 
            $_XPathTool.selectNodes( 
                $contentRoot, 
                "/system-index-block/system-page[dynamic-metadata[(name='letter' or name='second-letter') and value='A']]" ) )
    
  6. 6 Posted by Wing Ming Chan on 13 Jul, 2015 06:28 PM

    Wing Ming Chan's Avatar

    Lauren,

    I tested my code using the XML you supplied, with some faked additions, and it outputs the letters all right. Please make sure that you use and value='$alpha' in the code. Here is the faked data:

    <system-index-block current-time="1436811018665" name="specialty-index" type="content_type">
        <system-page id="2164e5a4ac1009d000cf095e31fa50d6">
            <name>index</name>
            <is-published>true</is-published>
            <last-published-on>1436808074151</last-published-on>
            <last-published-by>zung</last-published-by>
            <title>Ear, Nose & Throat</title>
            <created-by>zung</created-by>
            <created-on>1435079337365</created-on>
            <last-modified-by>lfraser</last-modified-by>
            <last-modified>1436807356978</last-modified>
            <display-name>My Title</display-name>
            <link>http://www.google.com/</link>
            <dynamic-metadata>
                <name>letter</name>
                <value>E</value>
            </dynamic-metadata>
            <dynamic-metadata>
                <name>first-spec</name>
                <value>Ear, Nose & Throat</value>
            </dynamic-metadata>
            <dynamic-metadata>
                <name>second-spec</name>
                <value>O</value>
            </dynamic-metadata>
            <dynamic-metadata>
                <name>second-letter</name>
                <value>Otolaryngology</value>
            </dynamic-metadata>
        </system-page>
        <system-page id="2164e5a4ac1009d000cf095e31fa50d6">
            <name>index</name>
            <is-published>true</is-published>
            <last-published-on>1436808074151</last-published-on>
            <last-published-by>zung</last-published-by>
            <title>Ear, Nose & Throat</title>
            <created-by>zung</created-by>
            <created-on>1435079337365</created-on>
            <last-modified-by>lfraser</last-modified-by>
            <last-modified>1436807356978</last-modified>
            <display-name>My Title</display-name>
            <link>http://www.google.com/</link>
            <dynamic-metadata>
                <name>letter</name>
                <value>A</value>
            </dynamic-metadata>
            <dynamic-metadata>
                <name>first-spec</name>
                <value>Ear, Nose & Throat</value>
            </dynamic-metadata>
            <dynamic-metadata>
                <name>second-spec</name>
                <value>O</value>
            </dynamic-metadata>
            <dynamic-metadata>
                <name>second-letter</name>
                <value>Otolaryngology</value>
            </dynamic-metadata>
        </system-page>
        <system-page id="2164e5a4ac1009d000cf095e31fa50d6">
            <name>index</name>
            <is-published>true</is-published>
            <last-published-on>1436808074151</last-published-on>
            <last-published-by>zung</last-published-by>
            <title>Ear, Nose & Throat</title>
            <created-by>zung</created-by>
            <created-on>1435079337365</created-on>
            <last-modified-by>lfraser</last-modified-by>
            <last-modified>1436807356978</last-modified>
            <display-name>My Title</display-name>
            <link>http://www.google.com/</link>
            <dynamic-metadata>
                <name>letter</name>
                <value>B</value>
            </dynamic-metadata>
            <dynamic-metadata>
                <name>first-spec</name>
                <value>Ear, Nose & Throat</value>
            </dynamic-metadata>
            <dynamic-metadata>
                <name>second-spec</name>
                <value>O</value>
            </dynamic-metadata>
            <dynamic-metadata>
                <name>second-letter</name>
                <value>Otolaryngology</value>
            </dynamic-metadata>
        </system-page>
        <system-page id="2164e5a4ac1009d000cf095e31fa50d6">
            <name>index</name>
            <is-published>true</is-published>
            <last-published-on>1436808074151</last-published-on>
            <last-published-by>zung</last-published-by>
            <title>Ear, Nose & Throat</title>
            <created-by>zung</created-by>
            <created-on>1435079337365</created-on>
            <last-modified-by>lfraser</last-modified-by>
            <last-modified>1436807356978</last-modified>
            <display-name>My Title</display-name>
            <link>http://www.google.com/</link>
            <dynamic-metadata>
                <name>letter</name>
                <value>A</value>
            </dynamic-metadata>
            <dynamic-metadata>
                <name>first-spec</name>
                <value>Ear, Nose & Throat</value>
            </dynamic-metadata>
            <dynamic-metadata>
                <name>second-spec</name>
                <value>O</value>
            </dynamic-metadata>
            <dynamic-metadata>
                <name>second-letter</name>
                <value>Otolaryngology</value>
            </dynamic-metadata>
        </system-page>
    </system-index-block>
    

    Here is the output:

    <h2>A</h2>
    <p>
        <a href="http://www.google.com/"> My Title</a>
    </p>
    <p>
        <a href="http://www.google.com/"> My Title</a>
    </p>
    <br />
    <h2>B</h2>
    <p>
        <a href="http://www.google.com/"> My Title</a>
    </p>
    <br />
    <br />
    <br />
    <h2>E</h2>
    <p>
        <a href="http://www.google.com/"> My Title</a>
    </p>
    <br />
    <br />
    

    Wing

  7. 7 Posted by lauren.fraser on 13 Jul, 2015 07:11 PM

    lauren.fraser's Avatar

    Would my version of Cascade make a difference?

    [cid:[email blocked]]

  8. 8 Posted by Ryan Griffith on 13 Jul, 2015 07:25 PM

    Ryan Griffith's Avatar

    Hi Lauren,

    Looking at your screenshot, I noticed your XPath looks incorrect where you are adding in the current letter in the loop. Specifically, you have {$alpha} and not ${alpha}.

    When you have a moment, try the following:

    $_XPathTool.selectNodes( 
                $contentRoot, 
                "/system-index-block/system-page[dynamic-metadata[(name='letter' or name='second-letter') and value='${alpha}']]" ) )
    

    Please let me know if you have any questions.

    Thanks!

  9. Tim closed this discussion on 11 Aug, 2015 07:07 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