Forum Discussion

Fortunato's avatar
Fortunato
Contributor
11 years ago

FreeMarker template error

Hi,

 

I am trying to render a list of featured topics like so:

<#assign featured=rest("/threads/featured?page_size=5").threads />
<#list featured as thread>
${thread.messages.topic.subject}<br />
</#list>

 

The XML structure looks something like:

- response

  - threads

    - thread

      - messages

        - topic

          - subject

With the subject node:
<subject type="string">FreeMarker template error</subject>

(@see /restapi/vc/threads/featured)

 

The error I receive is:

For "${...}" content: Expected a string or something automatically convertible to string (number, date or boolean), but this evaluated to a sequence+hash (wrapper: f.e.dom.NodeListModel):
==> thread.messages.topic.subject  [in template "preview" at line 3, column 3]

I've been reading up on the freemarker documentation but I can't work out what I'm missing. If someone can shed some light on this that is greatly appreciated :) Thanks in advance.

 

Fortunato

 

  • The issue you've got is that the 'featured' object is a single node ('threads'), with lots of 'thread' children. So at the moment, instead of iterating over each thread, it's iterating over the single top-level threads node.

     

    'thread.messages.topic.subject' in your code will return a collection of every subject in every thread - which is why you get the error saying it can't convert it to a string.

     

    If you change your code to this, it should work:

     

    <#assign featured=rest("/threads/featured?page_size=5").threads />
    <#list featured.thread as thread>
    ${thread.messages.topic.subject}<br />
    </#list>

     

  • The issue you've got is that the 'featured' object is a single node ('threads'), with lots of 'thread' children. So at the moment, instead of iterating over each thread, it's iterating over the single top-level threads node.

     

    'thread.messages.topic.subject' in your code will return a collection of every subject in every thread - which is why you get the error saying it can't convert it to a string.

     

    If you change your code to this, it should work:

     

    <#assign featured=rest("/threads/featured?page_size=5").threads />
    <#list featured.thread as thread>
    ${thread.messages.topic.subject}<br />
    </#list>

     

    • Fortunato's avatar
      Fortunato
      Contributor

      Thanks for the concise explanation Nathan. It must have been too long since I've done any XML file parsing that I couldn't come up with that.
      :smileyhappy: