Forum Discussion

PAULEM's avatar
PAULEM
Advisor
6 years ago

Unsolvable error trying to get tags for a message

I am trying to build a JSON-LD block for our message page and cannot - no matter what I try - get the tags associated with the parent message.  It's driving me nuts!  Here's my code:   <#assign...
  • luk's avatar
    6 years ago

    PAULEM 

    It would be helpful if you could post the actual freemarker error that you're getting, there is usually some information that can point in the direction of the problem. So I'm trying to "blindly" debug your issue =D...

    let's see, for example this part of your code:

    <#if var_usertags.data.items?has_content>
       <#list var_usertags.data.items as msgtag>
          <#assign utag = msgtag.text?string />
       </#list> 
    <#else>
       <#assign utag = "NONE" />
    </#if>

    probably won't do what you are intending to, why? Because you're re-assigning the utag variable each time you #list over your tags, therefore even if you had multiple tags, your utag variable will always just contain one, the LAST one! The code can be written a bit more elegantly (and hopefully working as intended) like this:

    <#assign utag = [] />
    <#list var_usertags.data.items>
        <#items as msgtag>
            <#assign utag = utag + [msgtag.text] />
        </#items>
    <#else>
        <#assign utag = ['NONE'] />
    </#list> 

    as you probably understood, the utag variable is now a sequence (think array...), when outputting it in your JS object, you'd simply do:

        "keywords": "${utag?join(' ')?js_string}",

    notice the ?js_string, might be useful in that context, more info here.

    you could also optimize your query a bit by just requesting the necessary information that you need, e.g. only the tag-text:

    <#assign usertagQuery = "SELECT text FROM tags WHERE messages.id = '${msg_id}'" />  

    but that's more a cosmetic thing, the query is correct as far as I can tell.