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:
[#macro html]
<!DOCTYPE html>
<html>
[#nested/]
</html>
[/#macro]
A page template calls the macro like this:
[@helpers.html]
<body>
Hello world!
</body>
[/@helpers.html]
The page then renders the following HTML:
<!DOCTYPE html>
<html>
<body>
Hello world!
</body>
</html>
Page Layout#
The following macros render the structure of each page:
htmlrenders the<html>element.headrenders the<head>element, including the<title>, CSS, JavaScript, and meta tags. Theheadmacro loads Prime.js, which the social login and CAPTCHA scripts need. When you replace theheadmacro, keep the Prime.js<script>tag.bodyrenders the<body>element.headerrenders the page header. On the account pages, the header includes a logout link.mainrenders the main content panel of the hosted login pages, with the logo, the page title, alerts, and the locale selector.footerrenders the footer of each page, including the "Powered by FusionAuth" link.accountMainrenders the main content area of the self-service account pages, with alerts and anaccountFooter.accountFooterrenders the locale selector and the action links at the bottom of an account page.accountMaincallsaccountFooter.accountPanelrenders a panel on the account pages with the user's profile image and name, plus an edit link whenshowEditis true.accountPanelFullrenders a full-width panel with an optional title on the account pages.localeSelectorrenders a drop-down that switches the page between English and the theme's additional locales.orSeparatorrenders a horizontal divider with the localized word "or".progressBarrenders an animated bar that fills overdurationSecondsseconds. The logout pages useprogressBar.codeBoxrenders preformatted text, such as a user's recovery codes.
accountMain Parameters#
actionURLThe 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.
actionTextThe text of the link in the footer. When actionURL is an array, pass an array of the same length.
actionDirectionThe 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.
wideWhen 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:
printErrorAlertsrenders analertfor each message in theerrorMessagestemplate variable.printInfoAlertsrenders analertfor each message in theinfoMessagestemplate variable.alertrenders one error or info message. Callalertdirectly to show a message anywhere on a page.errorsrenders the field errors for one form field. The form field macros callerrors. Callerrorsyourself only when you write your own form field markup.
alert Parameters#
messageThe message to display.
typeThe alert style. The value error renders an error alert. Any other value renders an info alert.
iconThe Font Awesome icon name, such as exclamation-circle or info-circle.
includeDismissButtonWhen 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:
structuredFormrenders 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 theoauth2/authorize.ftltemplate.hiddenrenders a hidden input. When you don't pass avalue, the macro reads the value of the request parameter with the same name.oauthHiddenFieldsrenders the hidden inputs that carry the OAuth request through a form submit, such asclient_id,redirect_uri,state, andscope. Every form in the OAuth workflow callsoauthHiddenFields.inputrenders a text, password, date, number, or checkbox input. A password input includes a button that shows the password, unless you setcanShowPasswordto false.selectrenders a drop-down from theoptionslist. For theuser.timezoneandregistration.timezonefields, the macro lists the time zones instead.textarearenders a multi-line text field.customFieldrenders a custom form field. The macro picks the field macro from the field's control type.passwordFieldrenders the password fields on the account edit page.passwordRulesrenders the tenant's password requirements as a list. Pass thepasswordValidationRulestemplate variable.scopeConsentFieldrenders 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 theresolveScopeMessagingfunction for the text.cimdClientDetailsAvailable since1.70.0renders 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 thecimdClientName,cimdClientUri,cimdClientHost,cimdRedirectHost,cimdRedirectIsLoopback, andcimdResourceHosttemplate variables. The macro renders nothing whenclientHostis empty, so other clients see no change.required_checkbox_iconrenders a checked, disabled checkbox.scopeConsentFielduses the icon for required scopes.cssTooltiprenders a tooltip that appears on hover or focus. Pass the text astext, or pass a message key askey.
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:
checkboxrenders one checkbox.checkbox_listrenders a checkbox for each value inoptions.radio_listrenders a radio button for each value inoptions.locale_selectrenders a drop-down of every locale FusionAuth supports. The drop-down allows one selection.
customField Parameters#
fieldThe form field to render, from the form's field list.
keyThe name of the input, such as user.firstName.
autofocusWhen true, the browser focuses the field when the page loads.
labelThe field label.
placeholderThe message key for the placeholder text.
leftAddonWhen 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#
fieldThe password form field.
showCurrentPasswordFieldWhen 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.
Buttons and Links#
The following macros render buttons and links:
buttonrenders a submit button. Thestyleparameter acceptsprimary,delete, or any other value for a plain button.buttonLinkrenders a link styled as a button.linkButtonrenders a button styled as a link.linkrenders a link that keeps the current OAuth request parameters, so the user stays in the same OAuth workflow. For example, the login page useslinkfor the forgot password link.logoutLinkrenders a link to/oauth2/logout. After logout, FusionAuth sends the user toredirectURIwith the current OAuth request parameters.
link and logoutLink Parameters#
urllink only. The path of the link, such as /password/forgot.
redirectURIlogoutLink only. The URL to send the user to after logout.
extraParametersQuery 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:
[@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:
[@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:
appleButtonfor Apple.epicButtonfor Epic Games.facebookButtonfor Facebook.googleButtonfor Google, when Google uses theUseRedirectlogin method.googleGsiButtonfor Google, when Google uses the Google Identity Services API.linkedInButtonfor LinkedIn.nintendoButtonfor Nintendo.openIDConnectButtonfor OpenID Connect.samlv2Buttonfor SAML v2.sonypsnButtonfor Sony PlayStation Network.steamButtonfor Steam.twitchButtonfor Twitch.twitterButtonfor Twitter.xboxButtonfor 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"anddata-federated-csrf="${federatedCSRFToken}". The identity provider JavaScript reads the CSRF token from this element. - Give each button the
login-buttonclass and adata-identity-provider-idattribute 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 isUseRedirect. - For Apple, add the
appleclass. For Facebook and Twitter without a redirect, add thefacebookortwitterclass. These buttons also need the data attributes that theappleButton,facebookButton, andtwitterButtonmacros render, such asdata-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:
<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:
captchaScriptsadds the CAPTCHA provider's JavaScript. CallcaptchaScriptsinside theheadmacro.captchaBadgerenders the CAPTCHA widget or badge, plus any CAPTCHA error. CallcaptchaBadgeinside the form.
For more information on the template variables, see Template Variables.
Parameters#
Both macros take the following parameters:
captchaMethodThe type of CAPTCHA to use. The default theme passes the tenant.captchaConfiguration.captchaMethod template variable. The valid values are:
GoogleRecaptchaV2- use Google reCAPTCHA v2GoogleRecaptchaV3- use Google reCAPTCHA v3HCaptcha- use hCaptchaHCaptchaEnterprise- use hCaptcha Enterprise v25
showCaptchaWhen true, the macro renders the CAPTCHA. When false, the macro renders nothing. The default theme passes the showCaptcha template variable.
siteKeyThe 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.
[#if captchaMethod == "GoogleRecaptchaV2"]
<div class="g-recaptcha" data-sitekey="${siteKey!''}"
data-size="invisible"
data-callback="reCaptchaV2InvisibleCallback"
></div>
...
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")}:
displayreturns the value of a property on an object, such asdisplay(user, "name"). The function formats numbers with thousands separators. When the property is empty, the function returns thedefaultargument, which defaults to an en dash.resolveScopeMessagingreturns 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 thedefaultargument.addDataAttributesturns a map into HTMLdata-attributes.structuredFormusesaddDataAttributesfor itsdataAttributesparameter.
The defaultIfNull macro renders text, or default when text is missing.
Related
Learn about the APIs for creating, retrieving, updating, and deleting hosted login page themes.
A list of FusionAuth themes examples.
Learn how to make the FusionAuth hosted login pages match a React application.
Learn how to style the FusionAuth hosted login pages with Tailwind CSS.