Macros

Macros allows you to add some dynamic information in your texts.

Defining a macro

All macros are found under the macros directory of your modules. They respect a simple rule: the filename of the macro defines its name.

<module>WEB-INF/macros/username.groovy

This define a macro name username.

How to use a macro

It is as simple as putting in your text a call like this ##macro_name##. Macros does not take any parameters.

Dear ##username##,

This will render something like, "Dear John Doe," based on the user rendering the page (you have to check the option perUser for the cache in the options panel of your content to be sure that each user see his own name).

How to write a macro

For the macro you can use any scripting language JSR-223 compliant deploy on your platform (by default Groovy, Velocity and Freemarker).

username.groovy

if (currentUser.username.trim().equals("guest")) {
    print PrincipalViewHelper.getUserDisplayName(currentUser.username.trim());
} else {
    String property1 = currentUser.getProperty("j:firstName")
    if (property1 != null)
        print(property1.capitalize() + " ");
    String property2 = currentUser.getProperty("j:lastName")
    if (property2 != null)
        print(property2.capitalize())
    if (property1 == null && property2 == null)
        print(currentUser.getUsername().capitalize())
}

Default bindings available in your macros

NameClassDescription
currentNodeorg.jahia.services.content.JCRNodeWrapperThe node we are currently rendering
currentUserorg.jahia.services.usermanager.JahiaUserThe user currently connected
currentAliasUserorg.jahia.services.usermanager.JahiaUserThe user currently rendered if not the one connected
renderContextorg.jahia.services.render.RenderContextThe current context for rendering
currentResourceorg.jahia.services.render.ResourceThe resource associated with the current node
urlorg.jahia.services.render.URLGeneratorAn URL generator allowing to create your own URL