macro to create alpha listing
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
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 Wing Ming Chan on 10 Jul, 2015 06:20 PM
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:
Good luck!
Wing
2 Posted by lauren.fraser on 10 Jul, 2015 07:28 PM
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 Posted by Wing Ming Chan on 10 Jul, 2015 07:39 PM
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 Posted by Ryan Griffith on 13 Jul, 2015 01:25 PM
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 Posted by lauren.fraser on 13 Jul, 2015 06:11 PM
Wing,
I get the
<br />
tags output, but not any content. Here is a sample from my index block: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.6 Posted by Wing Ming Chan on 13 Jul, 2015 06:28 PM
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:Here is the output:
Wing
7 Posted by lauren.fraser on 13 Jul, 2015 07:11 PM
Would my version of Cascade make a difference?
[cid:[email blocked]]
8 Posted by Ryan Griffith on 13 Jul, 2015 07:25 PM
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:
Please let me know if you have any questions.
Thanks!
Tim closed this discussion on 11 Aug, 2015 07:07 PM.