jueves, 25 de febrero de 2010

From HTML to XML and beyond!

What happens if you need to deliver same content but in different format? Sure you don't want to duplicate content. Well Magnolia CMS provides a feature that with little work lets you deliver same content in any format you need, it could be xml, or even html but optimized for newsletters.

How to do this? You need to select a template definition and add sub templates like in the image below.



This sample is using Magnolia CMS version 3.6 (feature also available for 4.x) with just one sub template defined that will render as xml when I request the page as url.xml and rendered as html when requested as url.html.

Because at the moment, sub templates do not work for paragraphs, in the template file that generates the xml content we will have to iterate through the paragraphs.

In my sample I used jsp, so I iterated main collection within a scriplet, by first getting the active page and then its children:

Content actPage = Resource.getActivePage(request);
if(actPage.hasContent("mainColumnParagraphs")) {
//some code
}


When using freemarker there is no need to use java:

[#list content.main?children as para]
[#assign paragraphContent=para]
[#if para.metaData.template='mgnlTextBox']
[#include "textBox.ftl" ]
[/#if]
[/#list]


One of my use cases was to generate a newsletter template for the web and a simpler one that would be supported by the mail clients.

Auto generate paragraphs

By default Magnolia CMS STK comes with a mechanism to auto generate paragraphs in main and extras areas. This is very useful when you have the need of a paragraph to be present in all pages using some template. That paragraph could be modifiable or not and could be initialized with some default parameters or not.

If we have a look at the class SingletonParagraphTemplateModel, we can see that in the execute method has two method calls, one to generate a single paragraph in the main area (see stkSiteMap template) and the other one to generate any number of paragraphs in the extras area.

See below the template configuration for the main area auto generated sitemap paragraph



Nice thing is that you can extend this class or implement your own to generate any paragraph in any area of the page.

Below is the template configuration for the extras area auto generated paragraphs.