Writing Generic Templates

Overview

This page describes how to create a generic template that can render any content type in Bloomreach Content.

When to Use

Use a generic template when you need to display multiple content types with a consistent layout. For example, a search results page may return a list of different content types. You may want to display common properties, such as the title and date, for each result. However, not all content types will have the same properties.

Prerequisites

Implementation

Freemarker Templates

Freemarker provides the ?? operator to check if a property exists before rendering it. This is useful when some content types may not have all properties.

The following example iterates over search results and renders the title and date if available:

<#list searchResults.items as bean> <@hst.link var="link" hippobean=bean.item /> <ul> <li> <a href="${link}">${bean.title?html}</a> <#if bean.date??> <p><@fmt.formatDate value=bean.date.time type="Date" pattern="MMMM d, yyyy h:mm a" /></p> </#if> </li> </ul> </#list>

To ensure that both date and date.time exist, use the following pattern:

<#if bean.date?? & bean.date.time??> <p><@fmt.formatDate value=bean.date.time type="Date" pattern="MMMM d, yyyy h:mm a" /></p> </#if>

JSP Templates

JSP does not have a built-in operator for checking property existence. Instead, use the isReadable function from the HST Tag Library to check if a property is accessible.

The following example renders the title and date for each search result, if the properties are available:

<c:forEach var="bean" items="${requestScope.searchResults.items}" > <hst:link var="link" hippobean="${bean.item}"/> <ul> <li> <a href="${link}"><c:out value="${bean.title}"/></a> <c:if test="${hst:isReadable(bean, 'date')}"> <p><fmt:formatDate value="${bean.date.time}" type="Date" pattern="MMMM d, yyyy h:mm a" /></p> </c:if> </li> </ul> </c:forEach>

The isReadable function supports nested properties. To check for both date and date.time, use:

<c:if test="${hst:isReadable(bean, 'date.time')}"> <fmt:formatDate value="${bean.date.time}" type="Date" pattern="MMMM d, yyyy h:mm a" /> </c:if>

Verification

  • Render the template with different content types.
  • Confirm that the template displays the title and date when available.
  • Verify that the template does not throw errors or display empty values when properties are missing.
Share Feedback
Page: /frontend/standard-components/writing-generic-jsp-pages
Section: Frontend
Category *
Writing Generic Templates | Bloomreach Content Documentation