CWA Tag Library

This contribution to SDL Tridion World involves a �CWA Tag Library� which can be used in CWA 1.5.x implementations, compatible with SDL Tridion 2011. The CWA Tag Library complements the CD Tag Library delivered with Tridion out-of-the-box.

There are two tags available to directly write out the content of one or multiple component presentations on the page. The CWA Tag Library also contains a tag that enables the access to page metadata, page custom metadata and component presentations on a page by using standard Java JSTL. This has been proven to be very powerful; for example when working with conditions to write out particular component presentations.

Installation

Place the cwa-taglib-1.1.jar CWA Taglib jar in the WEB-INF/lib/ folder of the web application. Place the cwa_tags.tld tag library descriptor in the WEB-INF/tags/ folder of the web application.
Add the taglib configuration to the jsp-config node of the application web.xml:

<jsp-config>
 <taglib>
  <taglib-uri>cwa_tags</taglib-uri>
  <taglib-location>/WEB-INF/tags/cwa_tags.tld</taglib-location>
 </taglib>
</jsp-config>

CWA Taglibs

Page Directive

To be able to use the CWA Taglib tags on the page, the taglib has to be declared at the top of the CWA JSP view:

<%@ taglib uri="cwa_tags" prefix="cwa" %>

ComponentPresentation Tag

The ComponentPresentation Tag can be used to write out the content of one component presentation placed on the page. The usage of the tag is:

<!-- Write out the content of the specified component presentation on the page -->
<cwa:ComponentPresentation componentURI="tcm:48-318676" templateURI="tcm:48-283775-32"/>

<!-- Write out the first component presentation on the page with the specified template uri -->
<cwa:ComponentPresentation templateURI="tcm:48-283775-32"/>

ComponentPresentations Tag

The ComponentPresentations Tag can be used to write out the content of multiple component presentations placed on the page. The usage of the tag is:

<!-- Write out the content of all component presentations on the page -->
<cwa:ComponentPresentations/>

<!-- Write out the content of all component presentations on the page with the specified template uri -->
<cwa:ComponentPresentations templateURI="tcm:48-283775-32"/>

SetBasePath Tag

The SetBasePath Tag stores the base path in a request attribute under a given variable name. The base path can be used in combination with the <base> html tag to specify a default URL and a default target for all links on a page. Can be access on the page by using standard JSLT. The usage of the tag is:

<!-- Store base path in a request attribute with the name 'basePath' -->
<cwa:SetBasePath var="basePath"/>

SetPageMeta Tag

The SetPageMeta Tag stores the CWA PageMeta object in a request attribute under a given variable name. This enables the ability to the page metadata, page custom metadata and the component presentations on a page by using standard JSTL. The provided Javadoc can be used as reference on how to access the properties of each object using JSTL Expression Language. The usage of the tag is:

<!-- Store page meta in a request attribute with the name 'pageMeta' -->
<cwa:SetPageMeta var="pageMeta"/>

Taglib examples:

Listed below is an example of a CWA JSP view containing a number of common use cases:

<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ page pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="cwa_tags" prefix="cwa" %>

<!-- Set variable basepath -->
<cwa:SetBasePath var="basePath"/>

<!-- Set variable page metadata -->
<cwa:SetPageMeta var="pageMeta"/>

<!-- Set variable page custom metadata -->
<c:set var="customMeta" value="${pageMeta.customMeta}"/>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-us" xml:lang="en-us">
<head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

 <!--  Set a base target for all links on a page -->
 <base href="<c:out value="${basePath}"/>" />

 <link href="./styles/default.css" rel="stylesheet" type="text/css"/>

 <!--  Output page title from page meta data -->
 <title><c:out value="${pageMeta.title}"/></title>

 <!-- If a name value pair exists in the custom meta data, write it to a meta tag -->
 <c:if test="${not empty customMeta['keywords']}"><meta name="keywords" content="<c:out value="${customMeta['keywords'].value}"/>" /></c:if>
 
</head>
<body>
 <div id="content">

  <!-- Output the content of the specified component presentation on the page -->
  <cwa:ComponentPresentation componentURI="tcm:48-318676" templateURI="tcm:48-283775-32"/>

  <!-- Output the first component presentation on the page with the specified template uri -->
  <cwa:ComponentPresentation templateURI="tcm:48-283775-32"/>

  <!-- Output the content of all component presentations on the page -->
  <cwa:ComponentPresentations/>

  <!-- Output the content of all component presentations on the page with the specified template uri -->
  <cwa:ComponentPresentations templateURI="tcm:48-283775-32"/>


  <!-- Some more advances examples to render out component presentations using JSTL: -->

  <!-- Iterate over all component presentation on the page -->
  <c:forEach var="componentPresentation" items="${pageMeta.componentPresentations}" varStatus="status">

   <!-- If the componentURI of the component presentation equals the specified value, output it's content -->
   <c:if test="${componentPresentation.componentURI == 'tcm:48-318676'}">
    <c:out value="${componentPresentation.content}" escapeXml="false"/>
   </c:if>

   <!-- If the componentTemplateURI of the component presentation equals the specified value, output it's content -->
   <c:if test="${componentPresentation.componentTemplateURI == 'tcm:48-283775-32'}">
    <c:out value="${componentPresentation.content}" escapeXml="false"/>
   </c:if>
   
   <!-- If the value of a metadata field of the component presentation's component equals the specified value, add a class to the div container -->
   <c:choose>
    <c:when test="${componentPresentation.componentMeta.customMeta['article_type'].value == 'Breaking News'}">
     <div class="breaking-news">
      <c:out value="${componentPresentation.content}" escapeXml="false"/>
     </div>     
    </c:when>
    <c:otherwise>
     <div>
      <c:out value="${componentPresentation.content}" escapeXml="false"/>
     </div>
    </c:otherwise>
   </c:choose>

   <!-- Alternate a class of the div container -->
   <c:choose>
    <c:when test="${status.index%2==0}">
     <div class="even_row">
      <c:out value="${componentPresentation.content}" escapeXml="false"/>
     </div>
    </c:when>
    <c:otherwise>
     <div class="odd_row">
      <c:out value="${componentPresentation.content}" escapeXml="false"/>
     </div>
    </c:otherwise>
   </c:choose>
   
  </c:forEach>
 </div>
 <div class="footer">
  <span>Page last published on: <c:out value="${pageMeta.lastPublicationDate}"/></span>
 </div>
</body>
</html>


Developer
:    Jeroen Suurd
Company
:    HintTech
License agreement
Download package
Tags
:    CWA,  taglib,  tag library,  content web application,  2011
Version
:   1.1
Source
:   Yes

About the Author
Jeroen Suurd
Tridion Consultant

Jeroen is a Certified Tridion Consultant working for SDL�s implementation partner HintTech.

HintTech

Most Popular Building Blocks

These are the most popular Building Blocks. Did you already get them?