CraftVariable

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

Craft defines the craft global template variable.

View source (opens new window)

# Public Properties

Property Description
app craft\web\Application, craft\console\Application, null (opens new window) – The Craft application class
behaviors (opens new window) yii\base\Behavior (opens new window) – List of behaviors attached to this component.
components (opens new window) array (opens new window) – The list of the component definitions or the loaded component instances (ID => definition or instance).
cp craft\web\twig\variables\Cp
io craft\web\twig\variables\Io
rebrand craft\web\twig\variables\Rebrand
routes craft\web\twig\variables\Routes

# app

Type
craft\web\Application, craft\console\Application, null (opens new window)
Default value
null

The Craft application class

View source (opens new window)

# cp

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

View source (opens new window)

# io

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

View source (opens new window)

# rebrand

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

View source (opens new window)

# routes

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

View source (opens new window)

# Public Methods

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

# __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 (opens new window)

Arguments

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 (opens new window)

Arguments

# addresses()

Returns a new address query (opens new window).

View source (opens new window)

Arguments

Returns

craft\elements\db\AddressQuery

# assets()

Returns a new asset query (opens new window).

View source (opens new window)

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:

'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() (opens new window) or be detached using detachBehavior() (opens new window). Anonymous behaviors can not be retrieved or detached.

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

View source (opens new window)

Returns

array (opens new window) – 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 (opens new window)

Arguments

Returns

boolean (opens new window) – Whether the property can be read

# categories()

Returns a new category query (opens new window).

View source (opens new window)

Arguments

Returns

craft\elements\db\CategoryQuery

# entries()

Returns a new entry query (opens new window).

View source (opens new window)

Arguments

Returns

craft\elements\db\EntryQuery

# globalSets()

Since
3.0.4

Returns a new global set query (opens new window).

View source (opens new window)

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 (opens new window)

# matrixBlocks()

Returns a new Matrix block query (opens new window).

View source (opens new window)

Arguments

Returns

craft\elements\db\MatrixBlockQuery

# query()

Since
3.0.19

Returns a new generic query.

View source (opens new window)

Returns

craft\db\Query

# tags()

Returns a new tag query (opens new window).

View source (opens new window)

Arguments

Returns

craft\elements\db\TagQuery

# users()

Returns a new user query (opens new window).

View source (opens new window)

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_INIT

Type
yii\base\Event (opens new window)

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

See also init()