Freemarker in Alfresco

One of the very crucial component of Alfresco is FTL(Freemarker) Alfresco heavily relies on FTL for its presentation layer of Alfresco share and webscript output file, Now before diving dip into how alfresco uses it internally and where exactly it is placed from architecture point of view let me introduce you to Freemarker.

What is FreeMarker?

Freemarker is a “template engine”; it basically allows developer to follow MVC pattern for application where we need to segregate generation of output HTML into two different layers i.e. designing and Programming. It is a generic tool to generate text output (anything from HTML to auto generated source code) based on templates. It’s a Java package, a class library for Java programmers. It’s not an application for end-users in itself, but something that programmers can embed into their products. With the increase in popularity of MVC pattern Freemarker also gets popular as it allows designer to concentrate on designing and programmer to concentrate on business logic. It works as follow

Freemarker Engine

*Html code designed by Designer

Ex.

<html>

<b>Hello how are you ${username}</b>

</html>

**Java objects which will be generated dynamically by programmer as a output of business processes

Ex. model.username=Tom

Bother will be combined by Freemarker engine and it will generate final HTML output for end users.

Ex.

Hello how are you Tom

This is how it works. Now in Alfresco it is used in explorer and share both.

How Freemarker is used in Alfresco Explorer?

In Alfresco explorer different template views can be attached to any node where you can extract the details of attached node and represent it in different forms.Tempates are also used for emails and different views. Out of box some of the views are available. All out of box view templates can be found under

Templates consist of a simple template text file that can be stored either on the ClassPath or in a Repository. The template files are executed against the default or a custom Data Model and the output is directed to an output stream such as an Alfresco document or servlet output or to a web-client page via a JSF UI component.

A JSP page can contain any number of UI Template components, and therefore display any number of different templates and associated models.

There are few out of box objects available to alfresco templates within alfresco context which helps to get details from repository.

Some of these root-level objects are:

companyhome: The Company Home template node

userhome: The home space template node of the current user

person: The current user template node

document: The current document template node on which the template is running

space: The current space template node on which the template is running

session: Provides a current session ticket as the session.ticket value

How Freemarker is used in Alfresco Share?

Alfresco share is built on top of Spring surf frame work and spring surf framework heavily relies on webscripts. As we had seen in these previous posts how alfresco webscript uses FTL as their output file So ultimately full alfresco share UI is based on FTL only. Some of the root objects available in alfresco share are as follow.

context: The request context of the current page.

user: The current user.

content: The current content instance (available if the dispatcher is rendering a page for a given content object id).

page: Information relating to the current page object.

theme: The current theme ID.

instance: The currently rendering model object (along with rendering context).

sitedata: Utility for working with the Surf platform object model.

remote: The Web Script Framework remote helper object.

locale: The current Locale for the user request thread – as a string in Java Locale format.

htmlid: The page unique HTML ID for the current component if any.

url: URL model for the current page request.

head: Concatenated component .head template output.

Note: Alfresco share context does not provide direct access to Alfresco repository concepts such as Nodes and Properties that developers of repository tier Web Scripts will be familiar with.

Summary: In above article we had gone through what is FTL? How it is used in Alfresco Explorer and Alfresco Share. This is useful information for alfresco developers, architects and users.

Related

Author Spotlight

mitpatoliya

I love opensource technologies working with those technologies from the time I have stepped in to the Software Industry. Alfresco CMS is my area of expertise. I have worked on various complex implementations which involved integration of Alfresco with other technologies, extensively worked with JBPM workflows and Webscripts.