Creating a custom renderer for a Forms input

March 6, 2023

This document will describe how to create a custom renderer for a Forms input.

When saving a complex object to the value of an input, it is advised to create a renderer. A renderer will insure that the value of the input is displayed correctly and therefore is easily readable by the user.We will use the Rating Input as an example of how to implement your own renderer. 

In order to create a renderer, the first requirement is to create a definition that extends fcmix:renderer.

[fcnt:ratingRenderer] > jnt:content, mix:title, jmix:droppableContent, jmix:hiddenType, fcmix:renderer

Once the definition is in order, we can create the following structure in our project:

The .wzd file will be used by Forms to parse the component definition and the .jsp file will be the view for our renderer.

The contents of the .wzd file should reflect:

renderer {
   label "Rating Renderer"
   name "rating"
Naming strategy:
  • Label: name of input (camelcased if necessary) followed by Renderer‚Äč
  • Name: name of input (camelcased if necessary), value will be assigned to rendererName property of the created definition

The .jsp file houses the custom functionality of how to render the result of the input. For example, Rating renderer contains the following code:

Note: Render views are written using Underscore templates
<%@ taglib prefix="jcr" uri="" %>
<%@ taglib prefix="fmt" uri="" %>
<%@ taglib prefix="c" uri="" %>
<%@ taglib prefix="utility" uri="" %>
<%@ taglib prefix="template" uri="" %>
<%@ taglib prefix="functions" uri="" %>
<%@ taglib prefix="fn" uri="" %>
<%@ taglib prefix="form" uri="" %>
<%@ taglib prefix="query" uri="" %>

<%--@elvariable id="currentNode" type=""--%>
<%--@elvariable id="currentResource" type=""--%>
<%--@elvariable id="flowRequestContext" type="org.springframework.webflow.execution.RequestContext"--%>
<%--@elvariable id="out" type=""--%>
<%--@elvariable id="renderContext" type=""--%>
<%--@elvariable id="script" type=""--%>
<%--@elvariable id="scriptInfo" type="java.lang.String"--%>
<%--@elvariable id="url" type=""--%>
<%--@elvariable id="workspace" type="java.lang.String"--%>
<@ if (_.isEmpty(data.type) || data.type === "static") { for (rating=0; rating < data.value; rating++) {@>
<span><i class="fa fa_2x <@=data.css@>"></i></span>
<@}} else if (data.type === "variable") {@>
<span><i class="fa_2x <@=data.css@>"><p><@=data.value@></p></i></span>
<@} else {@>
<span><i class="fa fa_2x <@=data.css@>"></i></span>

The variable data represents the value that is saved to the input upon submission. Using Underscore's variable interpolation, we can retrieve and display any of the information that was provided as apart of our input’s value.

The final step in setting up our custom renderer, is that our input value must be an object and requires the property rendererName: nameOfOurRenderer. For example:

   rendererName "rating",
   value: value,

Upon viewing submission results, Forms will check whether there is a renderer defined in the value of the input. If rendererName property is detected, it will use the appropriate view to render the result.