Fixes #13996: Added yii\web\View::registerJsVar() method that allows registering JavaScript variables

This commit is contained in:
E.Alamo
2018-01-26 13:31:27 +01:00
committed by Alexander Makarov
parent 9aac304220
commit e07219c812
3 changed files with 51 additions and 0 deletions

View File

@ -516,6 +516,31 @@ class View extends \yii\base\View
}
}
/**
* Registers a JS code block defining a variable. The name of variable will be
* used as key, preventing duplicated variable names.
*
* @param string $name Name of the variable
* @param array|string $value Value of the variable
* @param int $position the position in a page at which the JavaScript variable should be inserted.
* The possible values are:
*
* - [[POS_HEAD]]: in the head section. This is the default value.
* - [[POS_BEGIN]]: at the beginning of the body section.
* - [[POS_END]]: at the end of the body section.
* - [[POS_LOAD]]: enclosed within jQuery(window).load().
* Note that by using this position, the method will automatically register the jQuery js file.
* - [[POS_READY]]: enclosed within jQuery(document).ready().
* Note that by using this position, the method will automatically register the jQuery js file.
*
* @since 2.0.14
*/
public function registerJsVar($name, $value, $position = self::POS_HEAD)
{
$js = sprintf('var %s = %s;', $name, \yii\helpers\Json::htmlEncode($value));
$this->registerJs($js, $position, $name);
}
/**
* Renders the content to be inserted in the head section.
* The content is rendered using the registered meta tags, link tags, CSS/JS code blocks and files.