Skip to main content

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

"1.0" encoding="UTF-8" ?>
"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">
  "text/html; charset=UTF-8" session="false" />
  "image">
    "imagedata" />
    "image" var="imageurl" />
    "${pageContext.request.contextPath}${imageurl}"
      class="contentImage_${imagedata.imageFloat}"

      alt="${imagedata.imageAlt}" />
 

  "title">
   


      "title" />
   


 

  "text" />

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.

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

      alt="${content.imageAlt}" />
 
if>
  if test="${not empty content.title}">
   


      ${content.title}
   


 
if>
  ${content.text}

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]
  "${contextPath}${content.image}" class="contentImage_${content.imageFloat}"
alt='${content.imageAlt!""}' />
[/#if]
[#if content.title?has_content]
 


    ${content.title}
 


[/#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 >>

Comments

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