Class yii\twig\Extension

Inheritanceyii\twig\Extension » Twig\Extension\AbstractExtension
Source Code https://github.com/yiisoft/yii2-twig/blob/master/Extension.php

Extension provides Yii-specific syntax for Twig templates.

Protected Properties

Hide inherited properties

Property Type Description Defined By

Public Methods

Hide inherited methods

Method Description Defined By
__construct() Creates new instance yii\twig\Extension
addUses() Adds namespaces and aliases from constructor yii\twig\Extension
beginWidget() Function for *_begin syntax support yii\twig\Extension
call() Calls a method yii\twig\Extension
endWidget() Function for *_end syntax support yii\twig\Extension
getFunctions() yii\twig\Extension
getName() yii\twig\Extension
getNodeVisitors() yii\twig\Extension
path() Generates relative URL yii\twig\Extension
registerAsset() Function for registering an asset yii\twig\Extension
registerAssetBundle() Function for additional syntax of registering asset bundles yii\twig\Extension
resolveAndCall() Resolves a method from widget and asset syntax and calls it yii\twig\Extension
resolveClassName() Resolves class name from widget and asset syntax yii\twig\Extension
setProperty() Sets object property yii\twig\Extension
url() Generates absolute URL yii\twig\Extension
viewHelper() Used for 'begin_page', 'end_page', 'begin_body', 'end_body', 'head' yii\twig\Extension
widget() Function for *_widget syntax support yii\twig\Extension

Property Details

Hide inherited properties

$aliases protected property

Used class aliases

protected array $aliases = []
$namespaces protected property

Used namespaces

protected array $namespaces = []
$widgets protected property

Used widgets

protected array $widgets = []

Method Details

Hide inherited methods

__construct() public method

Creates new instance

public void __construct ( array $uses = [] )
$uses array

Namespaces and classes to use in the template

                public function __construct(array $uses = [])
{
    $this->addUses($uses);
}

            
addUses() public method

Adds namespaces and aliases from constructor

public void addUses ( $args )
$args array

Namespaces and classes to use in the template

                public function addUses($args)
{
    foreach ((array)$args as $key => $value) {
        $value = str_replace('/', '\\', $value);
        if (is_int($key)) {
            // namespace or class import
            if (class_exists($value)) {
                // class import
                $this->aliases[StringHelper::basename($value)] = $value;
            } else {
                // namespace
                $this->namespaces[] = $value;
            }
        } else {
            // aliased class import
            $this->aliases[$key] = $value;
        }
    }
}

            
beginWidget() public method

Function for *_begin syntax support

public mixed beginWidget ( $widget, $config = [] )
$widget string

Widget name

$config array

Widget config

                public function beginWidget($widget, $config = [])
{
    $widget = $this->resolveClassName($widget);
    $this->widgets[] = $widget;
    return $this->call($widget, 'begin', [
        $config,
    ]);
}

            
call() public method

Calls a method

public mixed call ( $className, $method, $arguments null )
$className string

Class name

$method string

Method name

$arguments array

                public function call($className, $method, $arguments = null)
{
    $callable = [$className, $method];
    if ($arguments === null) {
        return call_user_func($callable);
    } else {
        return call_user_func_array($callable, $arguments);
    }
}

            
endWidget() public method

Function for *_end syntax support

public void endWidget ( $widget null )
$widget string

Widget name

                public function endWidget($widget = null)
{
    if ($widget === null) {
        if (empty($this->widgets)) {
            throw new InvalidCallException('Unexpected end_widget() call. A matching begin_widget() is not found.');
        }
        $this->call(array_pop($this->widgets), 'end');
    } else {
        array_pop($this->widgets);
        $this->resolveAndCall($widget, 'end');
    }
}

            
getFunctions() public method

public void getFunctions ( )

                public function getFunctions()
{
    $options = [
        'is_safe' => ['html'],
    ];
    $functions = [
        new TwigFunction('use', [$this, 'addUses'], $options),
        new TwigFunction('*_begin', [$this, 'beginWidget'], $options),
        new TwigFunction('*_end', [$this, 'endWidget'], $options),
        new TwigFunction('widget_end', [$this, 'endWidget'], $options),
        new TwigFunction('*_widget', [$this, 'widget'], $options),
        new TwigFunction('path', [$this, 'path']),
        new TwigFunction('url', [$this, 'url']),
        new TwigFunction('void', function(){}),
        new TwigFunction('set', [$this, 'setProperty']),
        new TwigFunction('t', [\Yii::class,'t']),
    ];
    $options = array_merge($options, [
        'needs_context' => true,
    ]);
    $functions[] = new TwigFunction('register_*', [$this, 'registerAsset'], $options);
    $functions[] = new TwigFunction('register_asset_bundle', [$this, 'registerAssetBundle'], $options);
    foreach (['begin_page', 'end_page', 'begin_body', 'end_body', 'head'] as $helper) {
        $functions[] = new TwigFunction($helper, [$this, 'viewHelper'], $options);
    }
    return $functions;
}

            
getName() public method

public void getName ( )

                public function getName()
{
    return 'yii2-twig';
}

            
getNodeVisitors() public method

public void getNodeVisitors ( )

                public function getNodeVisitors()
{
    return [
        new Optimizer(),
        new GetAttrAdjuster(),
    ];
}

            
path() public method

Generates relative URL

public string path ( $path, $args = [] )
$path string

The parameter to be used to generate a valid URL

$args array

Arguments

return string

The generated relative URL

                public function path($path, $args = [])
{
    if (is_array($path)) {
        $path = array_merge($path, $args);
    } elseif ($args !== []) {
        $path = array_merge([$path], $args);
    }
    return Url::to($path);
}

            
registerAsset() public method

Function for registering an asset

{{ use('yii/web/JqueryAsset') }}
{{ register_jquery_asset() }}
public mixed registerAsset ( $context, $asset )
$context array

Context information

$asset string

Asset name

                public function registerAsset($context, $asset)
{
    return $this->resolveAndCall($asset, 'register', [
        isset($context['this']) ? $context['this'] : null,
    ]);
}

            
registerAssetBundle() public method (available since version 2.0.4)

Function for additional syntax of registering asset bundles

{{ register_asset_bundle('yii/web/JqueryAsset') }}
public void|yii\web\AssetBundle registerAssetBundle ( $context, $bundle, $return false )
$context array

Context information

$bundle string

Asset bundle class fully qualified name

$return boolean

Indicates if AssetBundle should be returned

                public function registerAssetBundle($context, $bundle, $return = false)
{
    $bundle = str_replace('/', '\\', $bundle);
    $bundle = $this->call($bundle, 'register', [
        isset($context['this']) ? $context['this'] : null,
    ]);
    if ($return) {
        return $bundle;
    }
}

            
resolveAndCall() public method

Resolves a method from widget and asset syntax and calls it

public mixed resolveAndCall ( $className, $method, $arguments null )
$className string

Class name

$method string

Method name

$arguments array

                public function resolveAndCall($className, $method, $arguments = null)
{
    return $this->call($this->resolveClassName($className), $method, $arguments);
}

            
resolveClassName() public method

Resolves class name from widget and asset syntax

public string resolveClassName ( $className )
$className string

Class name

                public function resolveClassName($className)
{
    $className = Inflector::id2camel($className, '_');
    if (isset($this->aliases[$className])) {
        return $this->aliases[$className];
    }
    foreach ($this->namespaces as $namespace) {
        $resolvedClassName = $namespace . '\\' . $className;
        if (class_exists($resolvedClassName)) {
            return $this->aliases[$className] = $resolvedClassName;
        }
    }
    return $className;
}

            
setProperty() public method

Sets object property

public void setProperty ( $object, $property, $value )
$object stdClass
$property string
$value mixed

                public function setProperty($object, $property, $value)
{
    $object->$property = $value;
}

            
url() public method

Generates absolute URL

public string url ( $path, $args = [] )
$path string

The parameter to be used to generate a valid URL

$args array

Arguments

return string

The generated absolute URL

                public function url($path, $args = [])
{
    if (is_array($path)) {
        $path = array_merge($path, $args);
    } elseif ($args !== []) {
        $path = array_merge([$path], $args);
    }
    return Url::to($path, true);
}

            
viewHelper() public method

Used for 'begin_page', 'end_page', 'begin_body', 'end_body', 'head'

public void viewHelper ( $context, $name null )
$context array

Context information

$name string

                public function viewHelper($context, $name = null)
{
    if ($name !== null && isset($context['this'])) {
        $this->call($context['this'], Inflector::variablize($name));
    }
}

            
widget() public method

Function for *_widget syntax support

public mixed widget ( $widget, $config = [] )
$widget string

Widget name

$config array

Widget config

                public function widget($widget, $config = [])
{
    return $this->resolveAndCall($widget, 'widget', [
        $config,
    ]);
}