Using Liferay DXP's Macros in Your Theme

Macros can assign theme template fragments to a variable. This keeps your theme templates from becoming cluttered and makes them easier to read. Liferay DXP defines several macros in FTL_Liferay.ftl template that you can use in your FreeMarker theme templates to include theme resources, standard portlets, and more. Liferay DXP also exposes its taglibs as FreeMarker macros. See the corresponding taglib tutorial for more information on using the taglib in your FreeMarker templates. This tutorial shows how to use macros in your FreeMarker theme templates.

Follow these steps:

  1. Select one of the macros shown in the table below:

    MacroParametersDescriptionExample
    breadcrumbsdefault_preferencesAdds the Breadcrumbs portlet with optional preferences<@liferay.breadcrumbs />
    control_menuN/AAdds the Control Menu portlet<@liferay.control_menu />
    cssfile_nameAdds an external stylesheet with the specified file name location<@liferay.css file_name="${css_folder}/mycss.css"/>
    dateformatPrints the date in the current locale with the given format<@liferay.date format="/yyyy/MM/dd/HH/" />
    jsfile_nameAdds an external JavaScript file with the specified file name source<@liferay.js file_name="${javascript_folder}/myJs.js"/>
    languagekeyPrints the specified language key in the current locale<@liferay.language key="last-modified" />
    language_formatarguments
    key
    Formats the given language key with the specified arguments. For example, passing go-to-x as the key and Mars as the arguments prints Go to Mars.<@liferay.language_format arguments="${site_name}" key="go-to-x" />
    languagesdefault_preferencesAdds the Languages portlet with optional preferences<@liferay.languages />
    navigation_menudefault_preferences
    instance_id
    Adds the Navigation Menu portlet with optional preferences and instance ID.<@liferay.navigation_menu />
    searchdefault_preferencesAdds the Search portlet with optional preferences<@liferay.search />
    search_bardefault_preferencesAdds the Search Bar portlet with optional preferences<@liferay.search_bar />
    user_personal_barN/AAdds the User Personal Bar portlet<@liferay.user_personal_bar />
  2. Call the macro in your theme template. Liferay DXP’s default FreeMarker macro calls are namespaced with liferay. For example, to include the Search Bar portlet, you would add the macro call shown below:

    <@liferay.search_bar>
    
  3. Include any required or optional arguments, such as portlet preferences, in the macro call. For example, Liferay DXP’s language macro directive includes a language key parameter:

    <#macro language
      key
    >
      ${languageUtil.get(locale, key)}
    </#macro>
    

    You can pass an argument in the macro call like this:

    <@liferay.language key="powered-by" />
    

    The example below sets the Search portlet’s Portlet Decorator to barebone:

    <@liferay.search default_preferences=
      freeMarkerPortletPreferences.getPreferences(
        "portletSetupPortletDecoratorId", "barebone"
      ) 
    />
    

    You can also pass multiple portlet preferences in an object, as shown in the example below for the Navigation Menu portlet:

    <#assign secondaryNavigationPreferencesMap = 
      {
        "displayStyle": "ddmTemplate_NAVBAR-BLANK-JUSTIFIED-FTL", 
        "portletSetupPortletDecoratorId": "barebone", 
        "rootLayoutType": "relative", 
        "siteNavigationMenuId": "0", 
        "siteNavigationMenuType": "1"
      } 
    />
    
    <@liferay.navigation_menu
      default_preferences=
      freeMarkerPortletPreferences.getPreferences(secondaryNavigationPreferencesMap)
      instance_id="main_navigation_menu"
    />
    

Now you know how to use Liferay DXP’s macros in your theme templates!

Creating Themes

Creating Reusable Pieces of Code for Your Themes

Theme Reference Guide

« Packaging Independent UI Resources for Your SiteImporting Resources with a Theme »
Was this article helpful?
0 out of 0 found this helpful