Content items are typically used in two contexts:
This tutorial focuses on rendering the Content entries in a website, with the concept of views.
Jahia modules rely on Java and Maven. The key requirements are as follows:
By following the steps in this tuturial, you will learn about using views to render custom content types in Jahia.
A view determines how a content item will be displayed. A view is:
Several views can be attached to the same content type, and made available to content authors. Views are dynamic and can display content from various sources, and include other views to optimize code reusability. Views are cached by default.
For this tutorial, we are going to rely on the following content type definition and the module defined in the module management tutorial:
[jnt:introBlock] > jnt:content, jmix:structuredContent
- title (string) i18n
- description (string) i18n
- creationDate (date)
Views can be created in two ways:
Views are stored in Jahia modules, under the path src/main/resources
. An example of a view path would be src/main/resources/
jnt_introBlock
/html/
introBlock
.jsp
Where:
<nodetypePrefix>_<nodetypeName>
<nodetypeName>
Create this directory structure and an empty View file for your content definition.
The base content of a view is as follows, and can be copied and pasted to the view file you created.
<%@ page language="java" contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="template" uri="http://www.jahia.org/tags/templateLib" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="jcr" uri="http://www.jahia.org/tags/jcr" %>
<%@ taglib prefix="ui" uri="http://www.jahia.org/tags/uiComponentsLib" %>
<%@ taglib prefix="functions" uri="http://www.jahia.org/tags/functions" %>
<%@ taglib prefix="query" uri="http://www.jahia.org/tags/queryLib" %>
<%@ taglib prefix="utility" uri="http://www.jahia.org/tags/utilityLib" %>
<%@ taglib prefix="s" uri="http://www.jahia.org/tags/search" %>
<%--@elvariable id="currentNode" type="org.jahia.services.content.JCRNodeWrapper"--%>
<%--@elvariable id="out" type="java.io.PrintWriter"--%>
<%--@elvariable id="script" type="org.jahia.services.render.scripting.Script"--%>
<%--@elvariable id="scriptInfo" type="java.lang.String"--%>
<%--@elvariable id="workspace" type="java.lang.String"--%>
<%--@elvariable id="renderContext" type="org.jahia.services.render.RenderContext"--%>
<%--@elvariable id="currentResource" type="org.jahia.services.render.Resource"--%>
<%--@elvariable id="url" type="org.jahia.services.render.URLGenerator"--%>
Understanding the elements:
<%@
taglib
is a list of available taglibs (functions) available to the view builders<%--@elvariable
are comments used by some IDEs (Intellij…) for automatic resolution of variables implicitly declared in the JSP. They will enable auto completion within the IDE for any variables declared like this.Hardcode Hello world!
into your view.
<%@ page language="java" contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="template" uri="http://www.jahia.org/tags/templateLib" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="jcr" uri="http://www.jahia.org/tags/jcr" %>
<%@ taglib prefix="ui" uri="http://www.jahia.org/tags/uiComponentsLib" %>
<%@ taglib prefix="functions" uri="http://www.jahia.org/tags/functions" %>
<%@ taglib prefix="query" uri="http://www.jahia.org/tags/queryLib" %>
<%@ taglib prefix="utility" uri="http://www.jahia.org/tags/utilityLib" %>
<%@ taglib prefix="s" uri="http://www.jahia.org/tags/search" %>
<%--@elvariable id="currentNode" type="org.jahia.services.content.JCRNodeWrapper"--%>
<%--@elvariable id="out" type="java.io.PrintWriter"--%>
<%--@elvariable id="script" type="org.jahia.services.render.scripting.Script"--%>
<%--@elvariable id="scriptInfo" type="java.lang.String"--%>
<%--@elvariable id="workspace" type="java.lang.String"--%>
<%--@elvariable id="renderContext" type="org.jahia.services.render.RenderContext"--%>
<%--@elvariable id="currentResource" type="org.jahia.services.render.Resource"--%>
<%--@elvariable id="url" type="org.jahia.services.render.URLGenerator"--%>
Hello world!
From the Jahia UI, deploy your module and activate it on your test site. To refer to instructions check the module management tutorial.
In the Jahia contributor interface, from page composer, click +Add Conent. Select your content type and preview. It should display Hello world! For detailed instructions visit the Adding a page tutorial or Test your content type in the Content Definition tutorial
Dynamic content is aggregated along with the HTML on the server-side, and fetches data from the database. Jahia uses the Apache Jackrabbit framework to structure the data. The hierarchy and properties of the data structure are important to understand before displaying dynamic content. Please refer to the content type definition tutorial.
Content can be displayed in a View by using the Java Expression Language (EL). EL is simple. In order to display the property title
of the current node, you would add the following to your view HTML:
${currentNode.properties['title'].string}
Note the .string
at the end of the EL code. It indicates the format in which you want to render the content. Other formats exist, such as long
, date
, time
.
Now, as an exercise, using the above notation, edit your view such that it displays all the properties of your custom nodetype definition.
JSTL, or Java Standard Tag Library is used to call functions of any sort, including conditional structures, String and Date manipulation. They can also be used to call one of the many APIs exposed by Jahia, such as the query API or the search API.
This tutorial will focus on the conditional structures and date rendering for the sake of demonstration.
JSTL tags are imported at the beginning of a JSP file, assigned to a prefix, and can then be used anywhere in the rest of the file.
<%@ page language="java" contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:if test="${currentNode.properties['title'] != null}">
<h2>${currentNode.properties['title'].string}</h2>
</c:if>
In the previous example, the taglib http://java.sun.com/jsp/jstl/core
is associated with the prefix c
, and the function if
is used.
Another common use case of JSTLs is for date formatting using the formatDate function:
<%@ page language="java" contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<fmt:formatDate value="${creationDate.time}" pattern="MMMM" var="startDateMonth"/>
<fmt:formatDate value="${creationDate.time}" pattern="d" var="startDateDay"/>
<fmt:formatDate value="${creationDate.time}" pattern="yyyy" var="startDateYear"/>
<div class="date">Publication date: ${startDateYearyyyy}-${startDateMonthMMMM}-${startDateDayd}</div>
With conditional and formatting JSTL functions, you should be able to render any piece of content in a better way:
There is much more to JSTL than what is shown in this tutorial. Please refer to the appropriate documentation of JSTL library descriptions (.tld files) to learn more about the various available libraries.
It is possible to split a content type rendering into multiple files in order to keep things simple, and optimize re-usability of the code.
Additional views for the same content type can be created by following this convention: src/main/resources/jnt_introBlock
/html/
introBlock
.
viewName
.jsp
For instance, if we created the view:src/main/resources/jnt_introBlock/html/introBlock.title.jsp
. This could display the title of your view, as well as its HTML rendering. You could then include it from the main view of the content type with the following JSTL syntax:
<template:includes node="${currentNode}" view="title" />
There are multiple ways to include code in a JSP, some have caching implications. Caching is essential to making sure Jahia can handle thousands of concurrent visitors. Views are cached by default, and it is essential to understand the various features associated with caching in Jahia. Please read the Caching tutorial for more information.