cancel
Showing results for 
Search instead for 
Did you mean: 

Custom methods.

spazzm
Champ in-the-making
Champ in-the-making
Hi, I'm new to Alfresco and this forum, please be gentle.

I'm trying to add a custom method to Alfresco so that I can do the following in my templates:
${doSomething(document.some.property)}

If I've understood the explanation of custom methods in the Wiki correctly, this is done through FreeMarker custom methods.

I've created a simple TemplateMethodModel implementation and tested it within FreeMarker.

How do I go about adding it to Alfresco in a way that makes it available to templates?
4 REPLIES 4

kevinr
Star Contributor
Star Contributor
Hi,

At the moment adding your own custom freemarker template method requires you to change the following code:

org.alfresco.web.ui.repo.component.template.DefaultModelHelper
In this class we generate the model (a Map of named objects) that the template is executed against. Any custom methods need to be part of that map. The method is made available to your template code using the name of the object in the Map. If you look for these lines:

      // add custom method objects
      root.put("hasAspect", new HasAspectMethod());
      root.put("message", new I18NMessageMethod());
      root.put("dateCompare", new DateCompareMethod());
you'll see that we add our custom methods here. So if you add your own class to the source tree and then add it to the objects added at this point then your method will be made available to templates executing through the web-client. You will need to build Alfresco from source to do this at present. I think will we add a task to allow custom method to be added via config at a later date.

If you are writing your own code to call the underlying repository template service directly, then you will be building the model Map yourself anyway, in which case you will do exactly the same as above to add your custom method impl class to the map with the desired name.

Hope this helps,

Kevin

spazzm
Champ in-the-making
Champ in-the-making
Yes, that did the trick  Smiley Happy

ribz33
Champ on-the-rise
Champ on-the-rise
Hi, kevin

i seen that code was moved to buildDefaultModel in FreeMarkerProcessor.

I want to create my own freemarker method, is there a way now to do it without modify your code ?

kevinr
Star Contributor
Star Contributor
Unfortunately not - it's on my TODO list Smiley Happy but other stuff been higher priority for this release - like the WCM UI! Smiley Wink

In 2.0 we have a nice extension mechanism in place for adding new Java bean objects with new methods etc. to the JavaScript API - and I'd like to follow the same pattern for the FreeMarker API. This would allow you to add both helper objects (containing new functional units) and new method objects to FreeMarker via configuration only - let's hope it stays on the list for Alfresco 2.1…

Thanks,

Kevin