Velocity "assigning" values to dynamic-metadata if the field is blank

lauren.fraser's Avatar

lauren.fraser

02 Jul, 2015 04:44 PM

I've got a content-index block that I'm using to build a staff directory. Some of the content we are using is pulled from dynamic metadata fields. I've tried to account for some staff not having data in those fields, by checking to see if they are empty and using an #if statement.

related velocity:

#foreach ($doc in $docs)
  #set ($docsTitle=$doc.getChild("display-name"))
  #set ($docsPath = $doc.getChild("link").value)
  #set ($docName = $doc.getChild("name"))
  #set ($imageName = $doc.getChild("name").value)
  #set ($docLocation = $_XPathTool.selectSingleNode($doc, "dynamic-metadata[name='primary-location']/value").value)
  #set ($docSpecialty = $_XPathTool.selectSingleNode($doc, "dynamic-metadata[name='primary-specialty']/value").value)
  #set ($imageCheck = $_.locateFile("_common/images/providers/${imageName}.png"))

  <div class="top-Find-Doctor-container cf">
    <div class="Find-Doctor-left">                  
      <h2><a href="${docsPath}"> $_EscapeTool.xml($docsTitle.value)</a></h2>
      #set ($hasSpec = $_XPathTool.selectSingleNode($docs,"dynamic-metadata[name='primary-specialty' and value != '']/value"))
      #if ($hasSpec)
        <span>$_EscapeTool.xml($docSpecialty)</span>
      #end
#end

Output html:

<div class="top-Find-Doctor-container cf">
    <div class="Find-Doctor-left">
        <h2>
            <a href="site://1.citizensmemorial.com/providers/deanna-holeman"> DeAnna Holeman, LCSW</a>
        </h2>
        <span>Nephrology</span>

I can't figure out where the Nephrology value is coming from. I don't see it in the index block. If there is data in the dynamic-metadata field, it seems to be displaying the correct info. But when it is blank, I'm seeing random specialties assigned to that staff.

Sample xml from the index block:

<system-page id="5c62696aac1009d0017504cbd038a3f2">
        <name>deanna-holeman</name>
        <is-published>true</is-published>
        <last-published-on>1412021777506</last-published-on>
        <last-published-by>lfraser</last-published-by>
        <title>DeAnna Holeman, LCSW</title>
        <display-name>DeAnna Holeman, LCSW</display-name>
        <path>/providers/deanna-holeman</path>
        <site>1.citizensmemorial.com</site>
        <link>site://1.citizensmemorial.com/providers/deanna-holeman</link>
        <created-by>admin</created-by>
        <created-on>1410299226459</created-on>
        <last-modified-by>lfraser</last-modified-by>
        <last-modified>1434639415350</last-modified>
        <dynamic-metadata>
            <name>primary-location</name>
            <value>Dallas County Family Medical Center</value>
        </dynamic-metadata>
        <dynamic-metadata>
            <name>secondary-location</name>
            <value>Stockton Family Medical Center</value>
        </dynamic-metadata>
        <dynamic-metadata>
            <name>primary-specialty</name>
        </dynamic-metadata>
        <dynamic-metadata>
            <name>secondary-specialty</name>
        </dynamic-metadata>
    </system-page>
  1. 1 Posted by Eric L. Epps on 02 Jul, 2015 05:56 PM

    Eric L. Epps's Avatar

    My guess is that the previous item's specialty in the XML is Nephrology. Velocity doesn't scope variables, so you'll need to set the value to blank before populating it to avoid this problem.

    For example,

    #set ($docSpecialty = "" )
    #set ($docSpecialty = $_XPathTool.selectSingleNode($doc, "dynamic-metadata[name='primary-specialty']/value").value)
    
  2. 2 Posted by lauren.fraser on 02 Jul, 2015 06:22 PM

    lauren.fraser's Avatar

    Eric - Thanks! That did the trick.

  3. 3 Posted by Ryan Griffith on 02 Jul, 2015 07:50 PM

    Ryan Griffith's Avatar

    Hi Lauren,

    I believe the main issue is that you were using $docs as the context element as opposed to $doc, so you would always get a value regardless of the current loop item (ie $doc).

    I also wanted to note that you are already saving the value of that dynamic metadata field so you don't need the additional $hasSpec variable. For example:

    ...
    
    #set ($docSpecialty = $_XPathTool.selectSingleNode($doc, "dynamic-metadata[name='primary-specialty']/value").value)
    
    ...
    
    #if ($docSpecialty != "")
      <span>$_EscapeTool.xml($docSpecialty)</span>
    #end
    
    ...
    

    Please let me know if you have any questions.

    Thanks!

  4. lauren.fraser closed this discussion on 07 Jul, 2015 07:42 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