W
W
web-quest32018-01-29 11:51:19
Yii
web-quest3, 2018-01-29 11:51:19

How to correctly pass a parameter from YII2 d JS?

There is a widget, I registered its js, but I need to include variables from PHP (widget settings) in this js (which I connected via asset), how to do this, what to read?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Kim, 2018-01-29
@web-quest3

Directly:
for scalar types:
for objects you can use the json format:

$this->view->registerJs(sprintf(" var a = %s ", json_encode($o)));

O
OKyJIucT, 2018-01-29
@OKyJIucT

Include the necessary JS in the view file using a construct where you can insert the necessary values, for example, like this

<?php

$param = 111;

$this->registerJs("
    var a = {$param};
    console.log(a); // 111
");

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question