For each loop for single item in data definition that allows multiples

lauren.fraser's Avatar

lauren.fraser

22 Feb, 2013 05:32 PM

I've done for each loops before and I feel like I'm making this harder than it is, but I can't get it to work.
Here is my page XML

<xml>
<system-data-structure>
<job-info>
<job-number>612</job-number>
<job-category>ADMINCLER</job-category>
<job-category>ALL</job-category>
<job-category>NON CLIN</job-category>
<dept>CMH HOS HIM</dept>
<description>Maintains, assembles, and files various health care records and assures their administrative accuracy; actively and consistently contributes to the department operations and communications; behaves in a manner consistent with the mission and objectives of CMH and performs other duties as requested.</description>
<shift>Mon-Fri  8:00-4:30</shift>
<hours>Full Time position working 80 Hours/Period</hours>
<hours-info/>
<salary/>
<qualifications>
<education>HIGH SCHOOL DIPLOMA in GENERAL STUDIES</education>
<education>HS GRADUATE EQUIVALENCY in GENERAL STUDIES</education>
<work-experience/>
<skills>CPR-FF</skills>
<skills>MISC-TYPE</skills>
<skills>MTERM</skills>
</qualifications>
</job-info>
</system-data-structure>
</xml>

What I need is for my velocity format to loop through the multiples (like job-category, education and skills) and display each entry. When I look at my other formats that contain a foreach statement, it seems like they are looking at a group that allows multiples, then setting the variables for each item in the group and serializing them from there (as opposed to just a single text field that allows multiples).

Attached is the velocity I'm playing with. I know that I have to set the variable before my for each loop, so that I can tell it what to loop through, but then how do I define or set the variable that should be serialized, when it's the same as what is already defined to be looped through?

  1. 1 Posted by Ryan Griffith on 22 Feb, 2013 07:35 PM

    Ryan Griffith's Avatar

    Hi Lauren,

    The easiest thing to do when you're dealing with multiple elements is to use the _XPathTool to get the elements, this way you can also use XPath to only get elements that are not blank.

    I made some tweaks to your Format, see the attached file. One suggestion I have would be to make the categories and qualifications an unordered list instead of new lines in a paragraph to make things a bit more usable.

    Note: As of 6.10+, we recommend using the EscapeTool to escape the value of elements, instead of the SerializerTool (used more for WYSIWYG content).

    Please let me know if you have any questions.

    Thanks

  2. 2 Posted by lauren.fraser on 22 Feb, 2013 07:51 PM

    lauren.fraser's Avatar

    Ryan,
    Thanks for the updated script. I made the items with multiples unordered lists as you suggested.

    Also, thanks for the tip on the EscapeTool instead of the SerializerTool. I'll put it on my list to rework our existing scripts as I have time.

  3. 3 Posted by Ryan Griffith on 22 Feb, 2013 07:59 PM

    Ryan Griffith's Avatar

    Not a problem at all, Lauren. Glad to hear that did the trick.

    For EscapeTool vs SerializerTool, generally the rule of thumb is to use SerializerTool with a WYSIWYG field, otherwise use the EscapeTool. This also gives you more flexibility since the EscapeTool has different methods available depending on what you need to escape.

    Have a great day!

  4. Ryan Griffith closed this discussion on 22 Feb, 2013 07:59 PM.

Comments are currently closed for this discussion. You can start a new one.

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