The Source for Java Technology Collaboration
User: Password:
Register | Login help    

Search

Online Books:
java.net on MarkMail:


Templaters nirvana

Posted by rah003 on September 16, 2008 at 2:35 PM PDT

Just to give you a short peek under the hood of development going on for Magnolia 3.7, here come three different examples of writing paragraph templates for Magnolia. The example is actual textimage paragraph from the current samples.

The first code snippet shows how things have been done up until now with JSP paragraph renderer. And it will still work in 3.7, so don't worry, you don't need to throw away your existing templates

<?xml version="1.0" encoding="UTF-8" ?>
<jsp:root version="1.2" xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:cms="cms-taglib"

  xmlns:cmsu="cms-util-taglib" xmlns:c="http://java.sun.com/jsp/jstl/core">
  <jsp:directive.page contentType="text/html; charset=UTF-8" session="false" />
  <cms:ifNotEmpty nodeDataName="image">
    <cms:setNode var="imagedata" />
    <cms:out nodeDataName="image" var="imageurl" />
    <img src="${pageContext.request.contextPath}${imageurl}" 
      class="contentImage_${imagedata.imageFloat}"

      alt="${imagedata.imageAlt}" />
  </cms:ifNotEmpty>
  <cms:ifNotEmpty nodeDataName="title">
    <h2>
      <cms:out nodeDataName="title" />
    </h2>
  </cms:ifNotEmpty>
  <cms:out nodeDataName="text" />

</jsp:root>

The second example is the same paragraph, again written using JSP, but taking advantage of implicit objects made available to templates through EL, as will be from 3.7 on.

<?xml version="1.0" encoding="UTF-8" ?>
<jsp:root version="1.2" xmlns:jsp="http://java.sun.com/JSP/Page" 
xmlns:c="http://java.sun.com/jsp/jstl/core">
  <jsp:directive.page contentType="text/html; charset=UTF-8" session="false" />
  <c:if test="${not empty content.image}">
    <img src="${pageContext.request.contextPath}${content.image}" 
    class="contentImage_${content.imageFloat}"

      alt="${content.imageAlt}" />
  </c:if>
  <c:if test="${not empty content.title}">
    <h2>
      ${content.title}
    </h2>
  </c:if>
  ${content.text}

</jsp:root>

The last (and I'm personally most fond of that one) is again the same paragraph, but this time written using Freemarker and again taking advantage of the implicit objects that Magnolia makes available for you automatically.

[#if content.image?has_content]
  <img src="${contextPath}${content.image}" class="contentImage_${content.imageFloat}" 
alt='${content.imageAlt!""}' />
[/#if]
[#if content.title?has_content]
  <h2>
    ${content.title}
  </h2>
[/#if]
${content.text!""}

Another goodie worth mentioning here is jsp tag support that will be available in freemarker based templates from 3.7 on.

The last one is clear winner, at least for me. Even if you don't want to use Freemarker and like to stick to the JSP, you can make your template much cleaner by not obstructing it with extra tags to provide what is anyway always necessary - the content for the paragraph. I hope it will make writing templates much easier and let you concentrate more on the presentation layer then on the hacking access to the content nodes in the underlying JCR repository.

The implicit objects provided in Magnolia from version 3.7 are:

  • content
  • paragraphConfig
  • templateConfig
  • ctx (magnolia context)
  • actpage (current page)
  • aggregationState (aggregated state of the page with all the extra info accumulated about page while being processed)

All of the objects above are being exposed the way that no matter if dealing with subnode or node data you can access those as if they have been normal bean properties.

Related Topics >> Web Applications      
Comments
Comments are listed in date ascending order (oldest first)

Thanks for the comparison, the visual impact is striking. Too bad no syntax highlighting here, it would be even clearer.