Global Variables
The following global variables are available to Twig templates in Craft:
Variable | Description
-------- | -----------
_self
| The current template name.
_context
| The currently-defined variables.
_charset
| The current charset.
craft | A craft3:craft\web\twig\variables\CraftVariable object.
currentSite | The requested site.
currentUser | The currently logged-in user.
devMode | Whether Dev Mode is enabled.
Global set variables | Variables for each of the global sets.
loginUrl | The URL to the front-end Login page.
logoutUrl | The URL to the front-end Logout page.
now | The current date/time.
POS_BEGIN | The craft\web\View::POS_BEGIN constant.
POS_END | The craft\web\View::POS_END constant.
POS_HEAD | The craft\web\View::POS_HEAD constant.
POS_LOAD | The craft\web\View::POS_LOAD constant.
POS_READY | The craft\web\View::POS_READY constant.
setPasswordUrl | The URL to the front-end Reset Password page.
siteName | The name of the current site.
siteUrl | The base URL of the current site.
SORT_ASC | The SORT_ASC
PHP constant.
SORT_DESC | The SORT_DESC
PHP constant.
SORT_FLAG_CASE | The SORT_FLAG_CASE
PHP constant.
SORT_LOCALE_STRING | The SORT_LOCALE_STRING
PHP constant.
SORT_NATURAL | The SORT_NATURAL
PHP constant.
SORT_NUMERIC | The SORT_NUMERIC
PHP constant.
SORT_REGULAR | The SORT_REGULAR
PHP constant.
SORT_STRING | The SORT_STRING
PHP constant.T
systemName | The system name.
view | The app’s view
component.
craft
A craft3:craft\web\twig\variables\CraftVariable object, which provides access points to various helper functions and objects for templates.
craft.app
A reference to the main craft3:craft\web\Application instance (the thing you get when you type Craft::$app
in PHP code) is also available to templates via craft.app
.
Accessing things via craft.app
is considered advanced. There are more security implications than other Twig-specific variables and functions, and your templates will be more susceptible to breaking changes during major Craft version bumps.
Common Services
Some of the services commonly used in templates:
craft.app.request
– Request object with information about the current HTTP requestcraft.app.session
– Session object useful for getting and setting flash messagescraft.app.user
– User object representing the logged-in human (when applicable)craft.app.config.general
– GeneralConfig object of General Config Settingscraft.app.fields
– Fields service for accessing custom field detailscraft.app.sections
– Sections service for working with sections and entry typescraft.app.sites
– Sites service for getting site details
Examples:
{# get the value of an `email` query parameter or post field #}
{% set address = craft.app.request.getParam('email') %}
{# get the value of the `notice` flash message #}
{% set message = craft.app.session.getFlash('notice') %}
{# get the current user’s email address #}
{% set email = craft.app.user.email %}
{# is `devMode` enabled? #}
{% set isDevMode = craft.app.config.general.devMode %}
{# get a custom field by its `body` handle #}
{% set field = craft.app.fields.getFieldByHandle('body') %}
{# get all the sections for the current site #}
{% set sections = craft.app.sections.getAllSections() %}
{# get all the sites for the current Craft installation #}
{% set sites = craft.app.sites.allSites() %}
currentSite
The requested site, represented by a craft3:craft\models\Site object.
{{ currentSite.name }}
You can access all of the sites in the same group as the current site via currentSite.group.sites
:
<ul>
{% for site in currentSite.group.sites %}
<li><a href="{{ alias(site.baseUrl) }}">{{ site.name }}</a></li>
{% endfor %}
</ul>
currentUser
The currently-logged-in user, represented by a craft3:craft\elements\User object, or null
if no one is logged in.
{% if currentUser %}
Welcome, {{ currentUser.friendlyName }}!
{% endif %}
devMode
Whether the config3:devMode config setting is currently enabled.
{% if devMode %}
Craft is running in dev mode.
{% endif %}
loginUrl
The URL to your site’s login page, based on the config3:loginPath config setting.
{% if not currentUser %}
<a href="{{ loginUrl }}">Login</a>
{% endif %}
logoutUrl
The URL Craft uses to log users out, based on the config3:logoutPath config setting. Note that Craft will automatically redirect users to your homepage after going here; there’s no such thing as a “logout page”.
{% if currentUser %}
<a href="{{ logoutUrl }}">Logout</a>
{% endif %}
now
A DateTime object set to the current date and time.
Today is {{ now|date('M j, Y') }}.
POS_BEGIN
Twig-facing copy of the craft\web\View::POS_BEGIN constant.
POS_END
Twig-facing copy of the craft\web\View::POS_END constant.
POS_HEAD
Twig-facing copy of the craft\web\View::POS_HEAD constant.
POS_LOAD
Twig-facing copy of the craft\web\View::POS_LOAD constant.
POS_READY
Twig-facing copy of the craft\web\View::POS_READY constant.
setPasswordUrl
The URL to setPasswordRequestPath
if it’s set. (This wraps the path in siteUrl
.)
{% if setPasswordUrl %}
<a href="{{ setPasswordUrl }}">Reset password</a>
{% endif %}
siteName
The name of your site, as defined in Settings → Sites.
siteUrl
The URL of your site
SORT_ASC
Twig-facing copy of the SORT_ASC
PHP constant.
SORT_DESC
Twig-facing copy of the SORT_DESC
PHP constant.
SORT_FLAG_CASE
Twig-facing copy of the SORT_FLAG_CASE
PHP constant.
SORT_LOCALE_STRING
Twig-facing copy of the SORT_LOCALE_STRING
PHP constant.
SORT_NATURAL
Twig-facing copy of the SORT_NATURAL
PHP constant.
SORT_NUMERIC
Twig-facing copy of the SORT_NUMERIC
PHP constant.
SORT_REGULAR
Twig-facing copy of the SORT_REGULAR
PHP constant.
SORT_STRING
Twig-facing copy of the SORT_STRING
PHP constant.
systemName
The System Name, as defined in Settings → General.
view
A reference to the craft3:craft\web\View instance that is driving the template.
Global Set Variables
Each of your site’s global sets will be available to your template as global variables, named after their handle.
They will be represented as craft3:craft\elements\GlobalSet objects.