Using formatters in OpenCms

(Difference between revisions)
Jump to: navigation, search
(New page: = Abstract = This document explains the concept of formatters used in OpenCms since Version 8.0.0. This concept replaces the <cms:contenload> and <cms:contentacces> tag used in older OpenC...)
 
Line 20: Line 20:
  
 
=Formatter configuration=
 
=Formatter configuration=
Formatters are defined in the XML Schema Definition of the container page resource type. Formatters get defined in the <formatters> node within the <xsd:appinfo> node of the XSD of the content. A formatter defines which formatter JSP should be used to render the XML content.
+
Formatters are defined in the XML Schema Definition of the container page resource type. Formatters get defined in the <formatters> node within the <xsd:appinfo> node of the XSD of the content. A formatter names the JSP that should be used to render the XML content.
 
  <xsd:annotation>
 
  <xsd:annotation>
 
   <xsd:appinfo>
 
   <xsd:appinfo>
Line 56: Line 56:
  
 
=The <cms:formatter> tag=
 
=The <cms:formatter> tag=
 +
In the JSP that should render the XML content the <cms:formatter> tag is to be used.
 +
<cms:formatter var="content" val="value">
 +
  ...
 +
</cms:formatter>
 +
Within the <cms:formatter> tag the XML content can be accessed with EL
 +
<cms:formatter var="content" val="value">
 +
  &lt;div class="article">
 +
  &lt;h1>${content.value.Title}</h1>
 +
  &lt;p>${content.value.Text}</p>
 +
  &lt;/div>
 +
</cms:formatter>
 +
 +
=JSP Standard Context Bean=
 +
The Expression Language makes it possible to easily access application data stored in JavaBeans components.
 +
 +
==Taglib declaration==
 +
Make sure to declare the all used taglibs at the first lines of the JSP
 +
<%@page session="false" taglibs="c,cms" %>
 +
 +
==EL access to Bean methods==
 +
Within the <cms:formatter> tag the JSP Standard Context Bean can be accessed with EL.
 +
This Bean provides several useful methods like:
 +
* getContainer()
 +
* getPage()
 +
* getElement()
 +
* getRequestContext()
 +
In EL this turns to
 +
* ${cms.container}
 +
* ${cms.page}
 +
* ${cms.element}
 +
* ${cms.requestContext}
 +
 +
==Combined example==
 +
<%@page session="false" taglibs="c,cms" %>
 +
<cms:formatter var="content" val="value">
 +
  &lt;div class="article">
 +
  &lt;h1>${value.title}</h1>
 +
  ${cms.requestContext.uri}
 +
  ${cms.requestContext.currentUser.name}
 +
  ${cms.element.sitePath}
 +
  ${cms.container.width}
 +
  &lt;/div>
 +
</cms:formatter>

Revision as of 15:09, 29 July 2011

Contents

Abstract

This document explains the concept of formatters used in OpenCms since Version 8.0.0. This concept replaces the <cms:contenload> and <cms:contentacces> tag used in older OpenCms versions. It is not backwards compatible.

Template

A page layout is defined in the template JSP. A page can have multiple containers where XML content of different resource types can be moved to by Drag & Drop in the ADE. Containers can be defined by using the <cms:container> tag in your template JSP.

<html>
 <head>
  ...
  <cms:enable-ade />
 </head>
 <body>
  ...
  <cms:container name="LEFTCOLUMN" type="sidecolumn" width="200" />
  <cms:container name="CENTERCOLUMN" type="center" width="500" />
  <cms:container name="RIGHTCOLUMN" type="sidecolumn" width="200" />
  ...
 </body>
</html>

Formatter configuration

Formatters are defined in the XML Schema Definition of the container page resource type. Formatters get defined in the <formatters> node within the <xsd:appinfo> node of the XSD of the content. A formatter names the JSP that should be used to render the XML content.

<xsd:annotation>
 <xsd:appinfo>
  ...
  <formatters>
   <formatter minWidth="400" maxWidth="700" uri="path to formatter JSP" />
   ...
  </formatters>
 </xsd:appinfo>
</xsd:annotation>

The same XML Content can be rendered by different formatter JSPs. Which formatter JSP is used for which container can be configured in the resource type schema. The formatter is chosen by the given container width or container type.

<xsd:annotation>
 <xsd:appinfo>
  ...
  <formatters>
   <formatter minWidth="100" uri="path to formatter JSP" />
   <formatter minWidth="400" maxWidth="700" uri="path to formatter JSP" />
   ...
  </formatters>
 </xsd:appinfo>
</xsd:annotation>
<xsd:annotation>
 <xsd:appinfo>
  ...
  <formatters>
   <formatter type="center" uri="path to formatter JSP" />
   <formatter type="sidecolumn" uri="path to formatter JSP" />
   ...
  </formatters>
 </xsd:appinfo>
</xsd:annotation>

The <cms:formatter> tag

In the JSP that should render the XML content the <cms:formatter> tag is to be used.

<cms:formatter var="content" val="value">
 ...
</cms:formatter>

Within the <cms:formatter> tag the XML content can be accessed with EL

<cms:formatter var="content" val="value">
 <div class="article">
  <h1>${content.value.Title}</h1>
  <p>${content.value.Text}</p>
 </div>
</cms:formatter>

JSP Standard Context Bean

The Expression Language makes it possible to easily access application data stored in JavaBeans components.

Taglib declaration

Make sure to declare the all used taglibs at the first lines of the JSP

<%@page session="false" taglibs="c,cms" %>

EL access to Bean methods

Within the <cms:formatter> tag the JSP Standard Context Bean can be accessed with EL. This Bean provides several useful methods like:

  • getContainer()
  • getPage()
  • getElement()
  • getRequestContext()

In EL this turns to

  • ${cms.container}
  • ${cms.page}
  • ${cms.element}
  • ${cms.requestContext}

Combined example

<%@page session="false" taglibs="c,cms" %>
<cms:formatter var="content" val="value">
 <div class="article">
  <h1>${value.title}</h1>
  ${cms.requestContext.uri}
  ${cms.requestContext.currentUser.name}
  ${cms.element.sitePath}
  ${cms.container.width}
 </div>
</cms:formatter>
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox