Detect Preview or Channel Manager Request Context
Overview
This page explains how to determine if a page request is made in the context of a preview site or from the Experience manager (Channel Manager).
When to Use
Use these detection methods in your templates when you need to conditionally include HTML or logic based on whether the request is for a preview version of the site or originates from the Experience manager.
Methods
The HstRequestContext interface provides methods to identify the preview state of a request:
-
isPreview():
Returnstrueif the request is for preview content through a preview mount. A preview mount is any HST mount with the propertyhst:typeset topreview. This includes Experience manager requests, but is not limited to them. -
isChannelManagerPreviewRequest():
Returnstrueif the request is for preview content specifically through the Experience manager (Channel Manager).
Implementation Examples
JSP Template Example
Use the following code in a JSP template to set variables indicating the preview state:
<hst:defineObjects/> <c:set var="isPreview" value="${hstRequest.requestContext.preview}"/> <c:set var="isChannelManagerPreviewRequest" value="${hstRequest.requestContext.channelManagerPreviewRequest}"/>
Freemarker Template Example
Use the following code in a Freemarker template to assign variables for preview detection:
<@hst.defineObjects/> <#assign isPreview=hstRequest.requestContext.preview/> <#assign isChannelManagerPreviewRequest=hstRequest.requestContext.channelManagerPreviewRequest/>