mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-26 14:26:54 +08:00
Fixes #13996: Added yii\web\View::registerJsVar()
method that allows registering JavaScript variables
This commit is contained in:

committed by
Alexander Makarov

parent
9aac304220
commit
e07219c812
@ -4,6 +4,7 @@ Yii Framework 2 Change Log
|
|||||||
2.0.14 under development
|
2.0.14 under development
|
||||||
------------------------
|
------------------------
|
||||||
|
|
||||||
|
- Enh #13996: Added `yii\web\View::registerJsVar()` method that allows registering JavaScript variables (Eseperio, samdark)
|
||||||
- Enh #9771: Assign hidden input with its own set of HTML options via `$hiddenOptions` in activeFileInput `$options` (HanafiAhmat)
|
- Enh #9771: Assign hidden input with its own set of HTML options via `$hiddenOptions` in activeFileInput `$options` (HanafiAhmat)
|
||||||
- Bug #15536: Fixed `yii\widgets\ActiveForm::init()` for call `parent::init()` (panchenkodv)
|
- Bug #15536: Fixed `yii\widgets\ActiveForm::init()` for call `parent::init()` (panchenkodv)
|
||||||
- Enh #14806: Added $placeFooterAfterBody option for GridView (terehru)
|
- Enh #14806: Added $placeFooterAfterBody option for GridView (terehru)
|
||||||
|
@ -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.
|
* 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.
|
* The content is rendered using the registered meta tags, link tags, CSS/JS code blocks and files.
|
||||||
|
@ -20,6 +20,31 @@ class ViewTest extends TestCase
|
|||||||
{
|
{
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testRegisterJsVar()
|
||||||
|
{
|
||||||
|
$this->mockWebApplication([
|
||||||
|
'components' => [
|
||||||
|
'request' => [
|
||||||
|
'scriptFile' => __DIR__ . '/baseUrl/index.php',
|
||||||
|
'scriptUrl' => '/baseUrl/index.php',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$view = new View();
|
||||||
|
$view->registerJsVar('username', 'samdark');
|
||||||
|
$html = $view->render('@yiiunit/data/views/layout.php', ['content' => 'content']);
|
||||||
|
$this->assertContains('<script type="text/javascript">var username = "samdark";</script></head>', $html);
|
||||||
|
|
||||||
|
$view = new View();
|
||||||
|
$view->registerJsVar('objectTest', [
|
||||||
|
'number' => 42,
|
||||||
|
'question' => 'Unknown',
|
||||||
|
]);
|
||||||
|
$html = $view->render('@yiiunit/data/views/layout.php', ['content' => 'content']);
|
||||||
|
$this->assertContains('<script type="text/javascript">var objectTest = {"number":42,"question":"Unknown"};</script></head>', $html);
|
||||||
|
}
|
||||||
|
|
||||||
public function testRegisterJsFileWithAlias()
|
public function testRegisterJsFileWithAlias()
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user