Powered By Blogger

Monday, December 6, 2010

Freemarker default objects

TemplateContentServlet is the class responsible for exposing the default objects of the FTL. BaseTemplateContentServlet will have the code related to prepare the model and expose the content.


/**
* Build the model that to process the template against.
*


* The model includes the usual template root objects such as 'companyhome', 'userhome',
* 'person' and also includes the node specified on the servlet URL as 'space' and 'document'
*
* @param services ServiceRegistry
* @param req Http request - for accessing Session and url args
* @param templateRef NodeRef of the template itself
* @param nodeRef NodeRef of the space/document to process template against
*
* @return an object model ready for executing template against
*/
@SuppressWarnings("unchecked")
private Map getModel(ServiceRegistry services, HttpServletRequest req, NodeRef templateRef, NodeRef nodeRef)
{
// build FreeMarker default model and merge
Map root = buildModel(services, req, templateRef);

// put the current NodeRef in as "space" and "document"
root.put("space", nodeRef);
root.put("document", nodeRef);

// add URL arguments as a map called 'args' to the root of the model
Map args = new HashMap(8, 1.0f);
Enumeration names = req.getParameterNames();
while (names.hasMoreElements())
{
String name = (String)names.nextElement();
try
{
args.put(name, new String(req.getParameter(name).getBytes(), "UTF-8"));
}
catch (UnsupportedEncodingException err) {}
}
root.put("args", args);

// Add the image resolver
root.put(TemplateService.KEY_IMAGE_RESOLVER, imageResolver);

// method to allow client urls to be generated
root.put("url", new URLHelper(req));

return root;
}

No comments:

Post a Comment