How to pass object from HstComponent to Freemarker templates?

753 views
Skip to first unread message

Eric Nguyen personal

unread,
Mar 4, 2015, 4:35:53 AM3/4/15
to hippo-c...@googlegroups.com
I have a document type Sports.
Sports document type has a compound field link which links to an image folder.

CMS Console configuration:
component
/hst:hst/hst:configurations/myproject/hst:pages/sportspage/main
  + gallery [hst:component]
    - hst:componentclassname = com.gec.components.GalleryComponent
    - hst:template = gallery
template:
/hst:hst/hst:configurations/myproject/hst:templates
  + gallery [hst:template] - hst:renderpath = freemarker/myproject/gallery.ftl

In this examples, I have a GalleryComponent backing a gallery template.
In the GalleryComponent, I'm already to retrieve the images.
I've use to try pass the result object (which is a list of HippoGalleryImageSet objects) to the template, but I'm getting error.
----------

 

[INFO] [talledLocalContainer] ==> ${result.count} [on line 5, column 4 in WEB-INF/freemarker/scc/gallery.ftl]

[INFO] [talledLocalContainer] ----------

[INFO] [talledLocalContainer]  

[INFO] [talledLocalContainer] Java backtrace for programmers:

[INFO] [talledLocalContainer] ----------

[INFO] [talledLocalContainer] freemarker.template.TemplateException: Expected hash. result evaluated instead to freemarker.template.SimpleSequence on line 5, column 6 in WEB-INF/freemarker/scc/gallery.ftl.

[INFO] [talledLocalContainer] at freemarker.core.TemplateObject.invalidTypeException(TemplateObject.java:135)


For testing, I tried to access one of the  HippoGalleryImageSet in the ftl template and I can!

(see below)

What am I missing?


GalleryComponent.java
@ParametersInfo(type = EssentialsDocumentListComponentInfo.class)
public class GalleryComponent extends EssentialsListComponent {@Override
 
public void doBeforeRender(final HstRequest request,
 
final HstResponse response)
 
throws HstComponentException {
 l
.info("doBeforeRender");
 
 
HippoDocumentBean subject = request.getRequestContext().getContentBean(HippoDocumentBean.class);
 
Sports sports = (Sports) subject;
 
HippoFolderBean sportsGallery = (HippoFolderBean) sports.getGallery();
 
 
List<HippoGalleryImageSet> images = sportsGallery.getDocuments(HippoGalleryImageSet.class);
 
for (HippoGalleryImageSet image: images) {
 l
.info("Image is "+image.getFileName());
 
}
 request
.setAttribute("result", images);
 request
.setAttribute("document", images.get(0));
 
}
}


gallery.ftl
<#include "/WEB-INF/freemarker/include/imports.ftl">
<h5>gallery</h5>
${document}
<!-- this object is printed out: org.hippoecm.hst.content.beans.standard.HippoGalleryImageSet@f418cf9d --><#if result??>
<p>${result.count}</p>
</#if>
<ul id="images">
<#list result.items as item>
 <@hst.link var="img" hippobean=item/>
 
<li>
 
<img src="${img}" alt="${item.fileName}" />
 
</li>
</#list>
</ul>



Tobias Jeger

unread,
Mar 4, 2015, 5:35:24 AM3/4/15
to hippo-c...@googlegroups.com
Hi Eric,

it appears that "result" is of type List<>, so your FTL check should probably look at the "size" of the list, not the "count"?

cheers

Tobi

--
Hippo Community Group: The place for all discussions and announcements about Hippo CMS (and HST, repository etc. etc.)
 
To post to this group, send email to hippo-c...@googlegroups.com
RSS: https://groups.google.com/group/hippo-community/feed/rss_v2_0_msgs.xml?num=50
---
You received this message because you are subscribed to the Google Groups "Hippo Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hippo-communi...@googlegroups.com.
Visit this group at http://groups.google.com/group/hippo-community.
For more options, visit https://groups.google.com/d/optout.



--
Amsterdam - Oosteinde 11, 1017 WT Amsterdam
Boston - 101 Main Street, Cambridge, MA 02142

US +1 877 414 4776 (toll free)
Europe +31(0)20 522 4466
www.onehippo.com

Eric Nguyen personal

unread,
Mar 4, 2015, 8:56:25 PM3/4/15
to hippo-c...@googlegroups.com
Thanks but I've tried, neither result.size or result.size() works.

Regarding the loop, I made a mistake.
I changed <code><#list result.items as item></code> to <code><#list result as item></code> and the loop works.

Woonsan Ko

unread,
Mar 4, 2015, 9:08:02 PM3/4/15
to hippo-c...@googlegroups.com
You should use the size built-in like this:

${result?size}

ref) http://freemarker.org/docs/ref_builtins_sequence.html#ref_builtin_size
> In this examples, I have a *GalleryComponent* backing a gallery
> template.
> In the *GalleryComponent*, I'm already to retrieve the images.
> I've use to try pass the result object (which is a list of
> *HippoGalleryImageSet* objects) to the template, but I'm getting
> error.
> |
> ----------
>
>
>
> [INFO][talledLocalContainer]==>${result.count}[on line 5,column
> 4inWEB-INF/freemarker/scc/gallery.ftl]
>
> [INFO][talledLocalContainer]----------
>
> [INFO][talledLocalContainer]
>
> [INFO][talledLocalContainer]Javabacktrace forprogrammers:
>
> [INFO][talledLocalContainer]----------
>
> [INFO][talledLocalContainer]freemarker.template.TemplateException:Expectedhash.result
> evaluated instead to freemarker.template.SimpleSequenceon line
> 5,column 6inWEB-INF/freemarker/scc/gallery.ftl.
>
> [INFO][talledLocalContainer]at
> freemarker.core.TemplateObject.invalidTypeException(TemplateObject.java:135)
> |
>
>
> For testing, I tried to access one of the HippoGalleryImageSet
> in the ftl template and I can!
>
> (see below)
>
> What am I missing?
>
>
> *GalleryComponent.java*
> |
> @ParametersInfo(type =EssentialsDocumentListComponentInfo.class)
> publicclassGalleryComponentextendsEssentialsListComponent{@Override
> publicvoiddoBeforeRender(finalHstRequestrequest,
> finalHstResponseresponse)
> throwsHstComponentException{
> l.info("doBeforeRender");
>
> HippoDocumentBeansubject
> =request.getRequestContext().getContentBean(HippoDocumentBean.class);
> Sportssports =(Sports)subject;
> HippoFolderBeansportsGallery =(HippoFolderBean)sports.getGallery();
>
> List<HippoGalleryImageSet>images
> =sportsGallery.getDocuments(HippoGalleryImageSet.class);
> for(HippoGalleryImageSetimage:images){
> l.info("Image is "+image.getFileName());
> }
> request.setAttribute("result",images);
> request.setAttribute("document",images.get(0));
> }
> }
>
> |
>
>
> *gallery.ftl*
> |
> <#include "/WEB-INF/freemarker/include/imports.ftl">
> <h5>gallery</h5>
> ${document} <!-- this object is printed out:
> org.hippoecm.hst.content.beans.standard.HippoGalleryImageSet@f418cf9d
> --><#if result??>
> <p>${result.count}</p>
> </#if>
> <ulid="images">
> <#list result.items as item>
> <@hst.link var="img" hippobean=item/>
> <li>
> <imgsrc="${img}"alt="${item.fileName}"/>
> </li>
> </#list>
> </ul>
> |
>
>
>
>
> --
> Hippo Community Group: The place for all discussions and
> announcements about Hippo CMS (and HST, repository etc. etc.)
>
> To post to this group, send email to hippo-c...@googlegroups.com
> <javascript:>
> RSS:
> https://groups.google.com/group/hippo-community/feed/rss_v2_0_msgs.xml?num=50
> <https://groups.google.com/group/hippo-community/feed/rss_v2_0_msgs.xml?num=50>
> ---
> You received this message because you are subscribed to the
> Google Groups "Hippo Community" group.
> To unsubscribe from this group and stop receiving emails from
> it, send an email to hippo-communi...@googlegroups.com
> <javascript:>.
> <http://groups.google.com/group/hippo-community>.
> For more options, visit https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>.
>
>
>
>
> --
> Amsterdam - Oosteinde 11, 1017 WT Amsterdam
> Boston - 101 Main Street, Cambridge, MA 02142
>
> US +1 877 414 4776 (toll free)
> Europe +31(0)20 522 4466
> www.onehippo.com <http://www.onehippo.com/>
>
> --
> Hippo Community Group: The place for all discussions and announcements
> about Hippo CMS (and HST, repository etc. etc.)
>
> To post to this group, send email to hippo-c...@googlegroups.com
> RSS:
> https://groups.google.com/group/hippo-community/feed/rss_v2_0_msgs.xml?num=50
> ---
> You received this message because you are subscribed to the Google
> Groups "Hippo Community" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to hippo-communi...@googlegroups.com
> <mailto:hippo-communi...@googlegroups.com>.
> Visit this group at http://groups.google.com/group/hippo-community.
> For more options, visit https://groups.google.com/d/optout.


--
w....@onehippo.com www.onehippo.com
Boston - 745 Atlantic Ave, 8th Floor, Boston MA 02111
Amsterdam - Oosteinde 11, 1017 WT Amsterdam
Reply all
Reply to author
Forward
0 new messages