Skip to content

CraftVariable

Type
Class
Namespace
craft\web\twig\variables
Inherits
craft\web\twig\variables\CraftVariable » yii\di\ServiceLocator » yii\base\Component » yii\base\BaseObject
Implements
yii\base\Configurable
Since
3.0.0

Craft defines the craft global template variable.

View source

Public Properties

PropertyDescription
appcraft\web\Application, craft\console\Application, null – The Craft application class
behaviorsyii\base\Behavior – List of behaviors attached to this component.
categoryGroupscraft\web\twig\variables\CategoryGroups
componentsarray – The list of the component definitions or the loaded component instances (ID => definition or instance).
configcraft\web\twig\variables\Config
cpcraft\web\twig\variables\Cp
deprecatorcraft\web\twig\variables\Deprecator
elementIndexescraft\web\twig\variables\ElementIndexes
emailMessagescraft\web\twig\variables\EmailMessages
feedscraft\web\twig\variables\Feeds
fieldscraft\web\twig\variables\Fields
globalscraft\web\twig\variables\Globals
i18n\craft\web\twig\variables\I18n
iocraft\web\twig\variables\Io
rebrandcraft\web\twig\variables\Rebrand
requestcraft\web\twig\variables\Request
routescraft\web\twig\variables\Routes
sectionscraft\web\twig\variables\Sections
sessioncraft\web\twig\variables\UserSession
systemSettingscraft\web\twig\variables\SystemSettings
userGroupscraft\web\twig\variables\UserGroups
userPermissionscraft\web\twig\variables\UserPermissions

app

Type
craft\web\Application, craft\console\Application, null
Default value
null

The Craft application class

View source

categoryGroups

Type
craft\web\twig\variables\CategoryGroups
Default value
null

View source

config

Type
craft\web\twig\variables\Config
Default value
null

View source

cp

Type
craft\web\twig\variables\Cp
Default value
null

View source

deprecator

Type
craft\web\twig\variables\Deprecator
Default value
null

View source

elementIndexes

Type
craft\web\twig\variables\ElementIndexes
Default value
null

View source

emailMessages

Type
craft\web\twig\variables\EmailMessages
Default value
null

View source

feeds

Type
craft\web\twig\variables\Feeds
Default value
null

View source

fields

Type
craft\web\twig\variables\Fields
Default value
null

View source

globals

Type
craft\web\twig\variables\Globals
Default value
null

View source

i18n

Type
\craft\web\twig\variables\I18n
Default value
null

View source

io

Type
craft\web\twig\variables\Io
Default value
null

View source

rebrand

Type
craft\web\twig\variables\Rebrand
Default value
null

View source

request

Type
craft\web\twig\variables\Request
Default value
null

View source

routes

Type
craft\web\twig\variables\Routes
Default value
null

View source

sections

Type
craft\web\twig\variables\Sections
Default value
null

View source

session

Type
craft\web\twig\variables\UserSession
Default value
null

View source

systemSettings

Type
craft\web\twig\variables\SystemSettings
Default value
null

View source

userGroups

Type
craft\web\twig\variables\UserGroups
Default value
null

View source

userPermissions

Type
craft\web\twig\variables\UserPermissions
Default value
null

View source

Public Methods

MethodDescription
__call()Calls the named method which is not a class method.
__clone()This method is called after the object is created by cloning an existing one.
__construct()Constructor.
__get()Getter magic method.
__isset()Checks if a property value is null.
__set()Sets the value of a component property.
__unset()Sets a component property to be null.
assets()Returns a new asset query.
attachBehavior()Attaches a behavior to this component.
attachBehaviors()Attaches a list of behaviors to the component.
behaviors()Returns a list of behaviors that this component should behave as.
canGetProperty()Returns a value indicating whether a property can be read.
canSetProperty()Returns a value indicating whether a property can be set.
categories()Returns a new category query.
className()Returns the fully qualified name of this class.
clear()Removes the component from the locator.
detachBehavior()Detaches a behavior from the component.
detachBehaviors()Detaches all behaviors from the component.
ensureBehaviors()Makes sure that the behaviors declared in behaviors() are attached to this component.
entries()Returns a new entry query.
get()Returns the component instance with the specified ID.
getBehavior()Returns the named behavior object.
getBehaviors()Returns all behaviors attached to this component.
getComponents()Returns the list of the component definitions or the loaded component instances.
globalSets()Returns a new global set query.
has()Returns a value indicating whether the locator has the specified component definition or has instantiated the component.
hasEventHandlers()Returns a value indicating whether there is any handler attached to the named event.
hasMethod()Returns a value indicating whether a method is defined.
hasProperty()Returns a value indicating whether a property is defined for this component.
init()Initializes the object.
isLocalized()Returns whether this site has multiple locales.
locale()Gets the current language in use.
matrixBlocks()Returns a new Matrix block query.
off()Detaches an existing event handler from this component.
on()Attaches an event handler to an event.
query()Returns a new generic query.
set()Registers a component definition with this locator.
setComponents()Registers a set of component definitions in this locator.
tags()Returns a new tag query.
trigger()Triggers an event.
users()Returns a new user query.

__call()

Calls the named method which is not a class method.

This method will check if any attached behavior has the named method and will execute it if available.

Do not call this method directly as it is a PHP magic method that will be implicitly called when an unknown method is being invoked.

View source

Arguments

  • $name (string) – The method name
  • $params (array) – Method parameters

Returns

mixed – The method return value

Throws

__construct()

Constructor.

The default implementation does two things:

  • Initializes the object with the given configuration $config.
  • Call init().

If this method is overridden in a child class, it is recommended that

  • the last parameter of the constructor is a configuration array, like $config here.
  • call the parent implementation at the end of the constructor.

View source

Arguments

  • $config (array) – Name-value pairs that will be used to initialize the object properties

assets()

Returns a new asset query.

View source

Arguments

Returns

craft\elements\db\AssetQuery

behaviors()

Returns a list of behaviors that this component should behave as.

Child classes may override this method to specify the behaviors they want to behave as.

The return value of this method should be an array of behavior objects or configurations indexed by behavior names. A behavior configuration can be either a string specifying the behavior class or an array of the following structure:

php
'behaviorName' => [
    'class' => 'BehaviorClass',
    'property1' => 'value1',
    'property2' => 'value2',
]

Note that a behavior class must extend from \craft\web\twig\variables\Behavior. Behaviors can be attached using a name or anonymously. When a name is used as the array key, using this name, the behavior can later be retrieved using getBehavior() or be detached using detachBehavior(). Anonymous behaviors can not be retrieved or detached.

Behaviors declared in this method will be attached to the component automatically (on demand).

View source

Returns

array – The behavior configurations.

canGetProperty()

Returns a value indicating whether a property can be read.

A property can be read if:

  • the class has a getter method associated with the specified name (in this case, property name is case-insensitive);
  • the class has a member variable with the specified name (when $checkVars is true);
  • an attached behavior has a readable property of the given name (when $checkBehaviors is true).

View source

Arguments

  • $name (string) – The property name
  • $checkVars (boolean) – Whether to treat member variables as properties
  • $checkBehaviors (boolean) – Whether to treat behaviors' properties as properties of this component

Returns

boolean – Whether the property can be read

categories()

Returns a new category query.

View source

Arguments

Returns

craft\elements\db\CategoryQuery

entries()

Returns a new entry query.

View source

Arguments

Returns

craft\elements\db\EntryQuery

globalSets()

Since
3.0.4

Returns a new global set query.

View source

Arguments

Returns

craft\elements\db\GlobalSetQuery

init()

Initializes the object.

This method is invoked at the end of the constructor after the object is initialized with the given configuration.

View source

isLocalized()

DEPRECATED

Deprecated in 3.0.0. Use craft.app.isMultiSite instead

Returns whether this site has multiple locales.

View source

Returns

boolean

locale()

DEPRECATED

Deprecated in 3.0.0

Gets the current language in use.

View source

Returns

string

matrixBlocks()

Returns a new Matrix block query.

View source

Arguments

Returns

craft\elements\db\MatrixBlockQuery

query()

Since
3.0.19

Returns a new generic query.

View source

Returns

craft\db\Query

tags()

Returns a new tag query.

View source

Arguments

Returns

craft\elements\db\TagQuery

users()

Returns a new user query.

View source

Arguments

Returns

craft\elements\db\UserQuery

Events

EVENT_DEFINE_BEHAVIORS

Type
craft\events\DefineBehaviorsEvent

The event that is triggered when defining the class behaviors

See also behaviors()


EVENT_DEFINE_COMPONENTS

DEPRECATED

Deprecated in 3.0.0-beta.23

Type
craft\web\twig\variables\DefineComponentsEvent

The event that is triggered when defining the Service Locator components.

See also __construct()


EVENT_INIT

Type
yii\base\Event

The event that is triggered after the component's init cycle

See also init()