Class yii\twig\Extension
Inheritance | yii\twig\Extension » Twig\Extension\AbstractExtension |
---|---|
Source Code | https://github.com/yiisoft/yii2-twig/blob/master/src/Extension.php |
Extension provides Yii-specific syntax for Twig templates.
Public 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 |
withViewEndPage() | yii\twig\Extension |
Property Details
Method Details
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);
}
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;
}
}
}
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,
]);
}
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);
}
}
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');
}
}
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;
}
public void getNodeVisitors ( ) |
public function getNodeVisitors()
{
return [
new Optimizer(),
new GetAttrAdjuster(),
];
}
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);
}
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,
]);
}
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;
}
}
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);
}
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;
}
Sets object property
public void setProperty ( $object, $property, $value ) | ||
$object | stdClass | |
$property | string | |
$value | mixed |
public function setProperty($object, $property, $value)
{
$object->$property = $value;
}
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);
}
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'])) {
if ($name === 'end_page') {
$this->viewEndPage = true;
} else {
$this->call($context['this'], Inflector::variablize($name));
}
}
}
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,
]);
}
public void withViewEndPage ( ) |
public function withViewEndPage(): bool
{
return $this->viewEndPage;
}