Displaying XML content values on the frontend
The previous steps described how to define a new
XML content. This page shows how to create a frontend output of the element
values of an XML content file. The simple example
from step 1 is used for the
following explanation.
The first step is to create a JSP that generates the output of the elements
of the XML content. For the simple example, the following code is used to
generate the output: 1 <%@ page session="false" %>
2 <%@ taglib prefix="cms" uri="http://www.opencms.org/taglib/cms" %>
3 <cms:include property="template" element="head" />
4 <div class="element">
5 <cms:contentload collector="singleFile" param="${opencms.uri}" editable="true">
6 <h1><cms:contentshow element="Title" /></h1>
7 <cms:contentloop element="Teaser">
8 <p><i><cms:contentshow /></i></p>
9 </cms:contentloop>
10 <p><cms:contentshow element="Text" /></p>
11 </div>
12 </cms:contentload>
13 <cms:include property="template" element="foot" />
The following lines are important to create the
output:
- Line 5: The contentload tag loads the contents using the
specified file collector, in this
case to get a single file. The param attribute value passes information about the
file to display to the collector. It is possible to use macros in the
attribute values, here the macro ${opencms.uri}
means the URI of the requested
file.
- Line 6 and 10: Single element values are displayed using the contentshow tag by specifying the element
name to show.
- Line 7 to 9: To display elements that can occur more times, use
the contentloop tag and contentshow to display the single items while
looping.
More advanced topics are testing if an element is
available before displaying it. To learn about the possibilities, have a look at
the cms taglib documentation and the reference of the tag <cms:contentcheck ifexists="...">.
The second step is to attach the JSP that displays the
content to the XML content resources. To do this, you have to set the value
of the property "template-elements" either on each XML content file or on a
parent folder that contains XML contents of the same type. The value must be the
absolute path of the previously created JSP in the OpenCms Virtual File System,
e.g. /alkacon-documentation/documentation-xmlcontent/simpleexample/simplecontentdetails.jsp for
the simple example.
Displaying content lists
Now you are able to create detail views of XML contents. To create list
views, have a closer look at the JSPs creating the lists in the /xmlcontentdemo/ folder. The main difference to
the detail output is the usage of another collector in the contentload tag: <cms:contentload collector="allInFolderDateReleasedDesc" param="/xmlcontent/article_${number}.html|article|4" editable="true"/>
The given collector loops the first 4 files of the
file type article in a folder sorted by the release date. The parameter value specifies the folder, the
resource type and the count of resources to display.
There are other resource collectors available, for example:
- allInFolderNavPos: gets all resources of a folder
sorted by their NavPos property values.
- allInSubTree: gets all resources in the sub tree
- allInFolderPriorityDateDesc: sorts all resources in a
folder by their collector.priority property value, the release date is the
second sort critera.
- allInFolderPriorityTitleDesc: sorts all resources in a folder by their
collector.priority property value, the Title property value is the second sort
critera.
Have a look at the JSPs of the XML content demo pages to learn more
about listing XML contents.
|