Themes Helper Macros

On this page

The _helpers.ftl template in every advanced theme defines FreeMarker macros and functions that the page templates share. Page templates call a macro with the helpers prefix, such as [@helpers.main]. When you edit a macro in _helpers.ftl, the change applies to every page that calls the macro.

This page lists the macros and functions in the default theme. Macro names that start with an underscore, such as _input_text, are internal to other macros and aren't listed. The default _helpers.ftl template is the full reference for every macro. To view the template, edit a theme in the Admin UI and open the Helpers template.

How Macros Work#

Most layout macros wrap content with [#nested/]. FreeMarker inserts the content between a macro's opening and closing tags at the [#nested/] element. For example, the html macro has the following definition:

HTML helper
[#macro html]
<!DOCTYPE html>
<html>
  [#nested/]
</html>
[/#macro]

A page template calls the macro like this:

Example usage of HTML macro
[@helpers.html]
<body>
Hello world!
</body>
[/@helpers.html]

The page then renders the following HTML:

Example result
<!DOCTYPE html>
<html>
<body>
Hello world!
</body>
</html>

Page Layout#

The following macros render the structure of each page:

  • html renders the <html> element.
  • head renders the <head> element, including the <title>, CSS, JavaScript, and meta tags. The head macro loads Prime.js, which the social login and CAPTCHA scripts need. When you replace the head macro, keep the Prime.js <script> tag.
  • body renders the <body> element.
  • header renders the page header. On the account pages, the header includes a logout link.
  • main renders the main content panel of the hosted login pages, with the logo, the page title, alerts, and the locale selector.
  • footer renders the footer of each page, including the "Powered by FusionAuth" link.
  • accountMain renders the main content area of the self-service account pages, with alerts and an accountFooter.
  • accountFooter renders the locale selector and the action links at the bottom of an account page. accountMain calls accountFooter.
  • accountPanel renders a panel on the account pages with the user's profile image and name, plus an edit link when showEdit is true.
  • accountPanelFull renders a full-width panel with an optional title on the account pages.
  • localeSelector renders a drop-down that switches the page between English and the theme's additional locales.
  • orSeparator renders a horizontal divider with the localized word "or".
  • progressBar renders an animated bar that fills over durationSeconds seconds. The logout pages use progressBar.
  • codeBox renders preformatted text, such as a user's recovery codes.

accountMain Parameters#

actionURL
  • Type: String or Array
  • Optional

The URL of the link in the footer. Pass an array to render several links. FusionAuth adds client_id, tenantId, and redirect_uri to each URL when the URL doesn't already contain them.

actionText
  • Type: String or Array
  • Optional
  • Defaults to Go back

The text of the link in the footer. When actionURL is an array, pass an array of the same length.

actionDirection
  • Type: String
  • Optional
  • Defaults to back

The arrow on the footer link. The value back puts a left arrow before the text. Any other value puts a right arrow after the text.

wide
  • Type: Boolean
  • Optional
  • Defaults to false

When true, the content area uses a wider maximum width.

The rowClass and colClass parameters remain for compatibility with older themes. The default theme doesn't use them.

Alerts and Errors#

The main and accountMain macros call the following alert macros, so most pages don't need to call them directly:

  • printErrorAlerts renders an alert for each message in the errorMessages template variable.
  • printInfoAlerts renders an alert for each message in the infoMessages template variable.
  • alert renders one error or info message. Call alert directly to show a message anywhere on a page.
  • errors renders the field errors for one form field. The form field macros call errors. Call errors yourself only when you write your own form field markup.

alert Parameters#

message
  • Type: String
  • Required

The message to display.

type
  • Type: String
  • Required

The alert style. The value error renders an error alert. Any other value renders an info alert.

icon
  • Type: String
  • Required

The Font Awesome icon name, such as exclamation-circle or info-circle.

includeDismissButton
  • Type: Boolean
  • Optional
  • Defaults to true

When true, the alert includes a button that closes the alert.

Forms#

The following macros render forms and form fields. Each field macro renders a label, the field, and any errors for the field:

  • structuredForm renders a <form> with one section for fields and one for buttons. The macro calls the nested content twice, first with "formFields" and then with "buttons", so the nested content checks the section name. For an example, see the oauth2/authorize.ftl template.
  • hidden renders a hidden input. When you don't pass a value, the macro reads the value of the request parameter with the same name.
  • oauthHiddenFields renders the hidden inputs that carry the OAuth request through a form submit, such as client_id, redirect_uri, state, and scope. Every form in the OAuth workflow calls oauthHiddenFields.
  • input renders a text, password, date, number, or checkbox input. A password input includes a button that shows the password, unless you set canShowPassword to false.
  • select renders a drop-down from the options list. For the user.timezone and registration.timezone fields, the macro lists the time zones instead.
  • textarea renders a multi-line text field.
  • customField renders a custom form field. The macro picks the field macro from the field's control type.
  • passwordField renders the password fields on the account edit page.
  • passwordRules renders the tenant's password requirements as a list. Pass the passwordValidationRules template variable.
  • scopeConsentField renders the field for a requested OAuth scope on the OAuth consent prompt page. The macro picks the field type from the scope's consent mode, and uses the resolveScopeMessaging function for the text.
  • cimdClientDetails Available since 1.70.0 renders the name, host, client_uri, requested resource host, and redirect host of a CIMD client on the OAuth consent prompt page. The macro also warns the user when the redirect host is a loopback address. The default theme passes the cimdClientName, cimdClientUri, cimdClientHost, cimdRedirectHost, cimdRedirectIsLoopback, and cimdResourceHost template variables. The macro renders nothing when clientHost is empty, so other clients see no change.
  • required_checkbox_icon renders a checked, disabled checkbox. scopeConsentField uses the icon for required scopes.
  • cssTooltip renders a tooltip that appears on hover or focus. Pass the text as text, or pass a message key as key.

The following macros render the custom fields of an advanced registration form. Each macro takes a field parameter, which only the registration and account edit pages provide. customField calls these macros, so you rarely call them directly:

  • checkbox renders one checkbox.
  • checkbox_list renders a checkbox for each value in options.
  • radio_list renders a radio button for each value in options.
  • locale_select renders a drop-down of every locale FusionAuth supports. The drop-down allows one selection.

customField Parameters#

field
  • Type: Object
  • Required

The form field to render, from the form's field list.

key
  • Type: String
  • Required

The name of the input, such as user.firstName.

autofocus
  • Type: Boolean
  • Optional
  • Defaults to false

When true, the browser focuses the field when the page loads.

label
  • Type: String
  • Optional

The field label.

placeholder
  • Type: String
  • Optional

The message key for the placeholder text.

leftAddon
  • Type: String
  • Optional
  • Defaults to true

When true, the field shows the icon from the field's data.leftAddon value, or an info icon when the field has no value. Any other value hides the icon.

passwordField Parameters#

field
  • Type: Object
  • Required

The password form field.

showCurrentPasswordField
  • Type: Boolean
  • Optional
  • Defaults to false

When true, the macro adds a current password field. Pass true when the application requires the current password to change a password.

The macro renders a toggle that shows the password fields. When the field's confirm value is true, the macro adds a confirm password field. After a password error, the macro also renders passwordRules.

The following macros render buttons and links:

  • button renders a submit button. The style parameter accepts primary, delete, or any other value for a plain button.
  • buttonLink renders a link styled as a button.
  • linkButton renders a button styled as a link.
  • link renders a link that keeps the current OAuth request parameters, so the user stays in the same OAuth workflow. For example, the login page uses link for the forgot password link.
  • logoutLink renders a link to /oauth2/logout. After logout, FusionAuth sends the user to redirectURI with the current OAuth request parameters.
url
  • Type: String
  • Required

link only. The path of the link, such as /password/forgot.

redirectURI
  • Type: String
  • Required

logoutLink only. The URL to send the user to after logout.

extraParameters
  • Type: String
  • Optional

Query parameters to add to the end of the URL. Start the value with &, such as &showPasswordField=false.

Both macros use the nested content as the link text:

Example usage of the link macro
[@helpers.link url="${request.contextPath}/password/forgot"]${theme.message('forgot-your-password')}[/@helpers.link]

Social Login#

The _helpers.ftl template renders the buttons for the social loginLogin through an account the user already has with a consumer service, such as Google or Apple. and other identity providers configured for an application. The hosted login page and the registration page (/oauth2/register, available since FusionAuth 1.28.0) call the following macros:

Social login code
[@helpers.head]
  [@helpers.alternativeLoginsScript clientId=application.id identityProviders=identityProviders/]
  ...
[/@helpers.head]

[@helpers.body]
  ...
  [@helpers.alternativeLogins clientId=application.id identityProviders=identityProviders passwordlessEnabled=passwordlessEnabled bootstrapWebauthnEnabled=bootstrapWebauthnEnabled idpRedirectState=idpRedirectState federatedCSRFToken=federatedCSRFToken/]
[/@helpers.body]

alternativeLoginsScript adds the JavaScript for the configured identity providers to the <head> element. Without that JavaScript, the login buttons don't work.

alternativeLogins renders a button for each configured identity provider. The macro also renders the magic link button when passwordless login is on, and the passkey button when bootstrapWebauthnEnabled is true.

alternativeLogins calls a button macro for each identity provider type:

  • appleButton for Apple.
  • epicButton for Epic Games.
  • facebookButton for Facebook.
  • googleButton for Google, when Google uses the UseRedirect login method.
  • googleGsiButton for Google, when Google uses the Google Identity Services API.
  • linkedInButton for LinkedIn.
  • nintendoButton for Nintendo.
  • openIDConnectButton for OpenID Connect.
  • samlv2Button for SAML v2.
  • sonypsnButton for Sony PlayStation Network.
  • steamButton for Steam.
  • twitchButton for Twitch.
  • twitterButton for Twitter.
  • xboxButton for Xbox.

Each button macro except googleGsiButton calls idpButton. idpButton renders the <button> element, and each provider macro passes in the provider's icon as nested content. To change the icon or text of one provider's button, edit that provider's macro.

Use Your Own Social Login Buttons#

To replace the default buttons with your own markup, remove the [@helpers.alternativeLogins] call and write the buttons yourself. Keep the alternativeLoginsScript call in the <head> element. The identity provider JavaScript finds your buttons by class and data attributes, so each button needs the following:

  • Wrap the buttons in an element with id="login-button-container" and data-federated-csrf="${federatedCSRFToken}". The identity provider JavaScript reads the CSRF token from this element.
  • Give each button the login-button class and a data-identity-provider-id attribute set to the identity provider's Id.
  • For a provider that logs in with a redirect, add data-login-method="UseRedirect". Epic Games, LinkedIn, Nintendo, OpenID Connect, SAML v2, Sony PlayStation Network, Steam, Twitch, and Xbox always log in with a redirect. Google and Facebook log in with a redirect when the identity provider's login method is UseRedirect.
  • For Apple, add the apple class. For Facebook and Twitter without a redirect, add the facebook or twitter class. These buttons also need the data attributes that the appleButton, facebookButton, and twitterButton macros render, such as data-scope.

The form on the page must include a client_id input. The oauthHiddenFields macro adds one.

The following example renders a button for an OpenID Connect identity provider:

Custom OpenID Connect login button
<div id="login-button-container" data-federated-csrf="${federatedCSRFToken}">
  [#list identityProviders["OpenIDConnect"]![] as identityProvider]
    <button class="login-button openidconnect"
            data-identity-provider-id="${identityProvider.id}"
            data-login-method="UseRedirect">
      ${identityProvider.lookupButtonText(application.id)}
    </button>
  [/#list]
</div>

The Google Identity Services button comes from Google's own JavaScript, so the requirements above don't apply to that button. To change that button, edit the googleGsiButton macro. For the available attributes, see the Google Identity Services HTML reference.

CAPTCHA#

The _helpers.ftl template provides macros that add CAPTCHA challenges to a page. A page that shows a CAPTCHA calls both macros:

  • captchaScripts adds the CAPTCHA provider's JavaScript. Call captchaScripts inside the head macro.
  • captchaBadge renders the CAPTCHA widget or badge, plus any CAPTCHA error. Call captchaBadge inside the form.

For more information on the template variables, see Template Variables.

Parameters#

Both macros take the following parameters:

captchaMethod
  • Type: String
  • Required

The type of CAPTCHA to use. The default theme passes the tenant.captchaConfiguration.captchaMethod template variable. The valid values are:

  • GoogleRecaptchaV2 - use Google reCAPTCHA v2
  • GoogleRecaptchaV3 - use Google reCAPTCHA v3
  • HCaptcha - use hCaptcha
  • HCaptchaEnterprise - use hCaptcha Enterprise v25
showCaptcha
  • Type: Boolean
  • Required

When true, the macro renders the CAPTCHA. When false, the macro renders nothing. The default theme passes the showCaptcha template variable.

siteKey
  • Type: String
  • Optional

The site key for the CAPTCHA. The default theme passes the tenant.captchaConfiguration.siteKey template variable. When captchaMethod is GoogleRecaptchaV3, the CAPTCHA requires a site key.

Invisible reCAPTCHA#

To challenge a form submit without a checkbox on the form, use an invisible reCAPTCHA. Add the data-size and data-callback attributes to the element with the g-recaptcha class in the captchaBadge macro. In FusionAuth 1.46.0 and later, the default template includes these attributes in a comment.

Invisible tag
[#if captchaMethod == "GoogleRecaptchaV2"]
      <div class="g-recaptcha" data-sitekey="${siteKey!''}"
        data-size="invisible"
        data-callback="reCaptchaV2InvisibleCallback"
      ></div>
 ...
Note

Before FusionAuth 1.46.0, the form submit JavaScript also needs an update to handle an invisible reCAPTCHA. For more information, see the 1.46.0 release notes.

Functions#

The _helpers.ftl template also defines the following FreeMarker functions. Call a function inside an expression, such as ${helpers.display(user, "email")}:

  • display returns the value of a property on an object, such as display(user, "name"). The function formats numbers with thousands separators. When the property is empty, the function returns the default argument, which defaults to an en dash.
  • resolveScopeMessaging returns the consent message or detail for an OAuth scope. The function checks for an application-specific message, then a tenant-specific message, then a general message, then returns the default argument.
  • addDataAttributes turns a map into HTML data- attributes. structuredForm uses addDataAttributes for its dataAttributes parameter.

The defaultIfNull macro renders text, or default when text is missing.