Shared FreeMarker variables in Magnolia CMS

Shared FreeMarker variables in Magnolia CMS

To introduce a new global variable like stkfn or cmsfn into your FreeMarker templates you can add a new entry to the Magnolia shared variables configuration.

  1. In AdminCentral go to Cofiguration | server | rendering | freemarker | sharedVariables
  2. Create a new content node for example myfn (your functions will be called through this name)
  3. Create a new property class below the myfn node and set the value of the property to your class name including the packages. For example ch.crisi.MyTemplatingFunctions.class.

magnolia_shared_variables

From now on you’re able to call all the methods of your MyTemplatingFunctions class from  freemarker.

public class MyTemplatingFunctions {

    public String helloWorld() {
        return "Hello World!";
    }

}
${myfn.helloWorld()}