import java.util.List;
import org.hippoecm.hst.core.component.HstComponentException;
import org.hippoecm.hst.core.component.HstRequest;
import org.hippoecm.hst.core.component.HstResponse;
import org.hippoecm.hst.content.beans.standard.HippoDocumentBean;
import org.hippoecm.hst.content.beans.standard.HippoFolder;
import org.onehippo.cms7.essentials.components.EssentialsListComponent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DocumentListComponent extends EssentialsListComponent {
public static final Logger log = LoggerFactory.getLogger(DocumentListComponent.class);
@Override
public void doBeforeRender(final HstRequest request, final HstResponse response) throws HstComponentException {
try {
final HippoFolder folder = getHippoBeanForPath("news", HippoFolder.class);
final List<HippoDocumentBean> documents = folder.getDocuments();
if (documents != null) {
request.setAttribute("documents", documents);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
<ul>
<#list documents as item>
<@hst.link var="link" hippobean=item/>
<li><a href="${link}">${item.title}</a></li>
</#list>
</ul>
I needed a component to list all documents in a subfolder. It took a lot of hunting and piecing together to get working code so I wanted to share here.The component code returns all published documents in the specified subfolder (in this case "news"). There is no pagination or filtering:
import java.util.List;
import org.hippoecm.hst.core.component.HstComponentException;
import org.hippoecm.hst.core.component.HstRequest;
import org.hippoecm.hst.core.component.HstResponse;
import org.hippoecm.hst.content.beans.standard.HippoDocumentBean;
import org.hippoecm.hst.content.beans.standard.HippoFolder;
import org.onehippo.cms7.essentials.components.EssentialsListComponent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DocumentListComponent extends EssentialsListComponent {
public static final Logger log = LoggerFactory.getLogger(DocumentListComponent.class);
@Override
public void doBeforeRender(final HstRequest request, final HstResponse response) throws HstComponentException {
try {
final HippoFolder folder = getHippoBeanForPath("news", HippoFolder.class);
final List<HippoDocumentBean> documents = folder.getDocuments();
if (documents != null) {
request.setAttribute("documents", documents);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
And the code for the FreeMarker template used by the component (in my case I gave the document type a field called "title"):
<ul>
<#list documents as item>
<@hst.link var="link" hippobean=item/>
<li><a href="${link}">${item.title}</a></li>
</#list>
</ul>I hope this helps for someone looking for a similar solution. Suggestions for improvement are welcome.
--
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-community@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-community+unsubscribe@googlegroups.com.
Visit this group at https://groups.google.com/group/hippo-community.
For more options, visit https://groups.google.com/d/optout.