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>