How to pass arguments to JavaScript?

My goal is, passing a config variable from PHP to JavaScript. Registration of the script as usual.

$foo  = $this->configModel->get('foo', 'bar');
// How to pass $foo to the script?
$this->hook->on('template:layout:js', array('template' => "plugins/Foobar/Assets/js/Foobar.js"));

Appending via “?foo=bar” doesn’t seem to work.

Maybe I’m searching too far. Is this possible at all?

Any hint is appreciated.

im not 100% sure of your context, but you can pull the config value into, say a hidden form value, and then grab that value in js. it doesnt have to be a form, could be anything.

the other easy option is cookies.

if you show me code it might be easier for me to assist.

I used it this way…

So I learnt, as I am in the middle of trying it again… you can do js inside php files with the variables but not in a JS file (add php variables)

I decided to use a cookie, even if I don’t like to.

What you need to understand is that nginx will only forward the *.php file to the php interpreter, and the js file nginx will return it as a static resource file, so the php variable in the js file cannot be recognized without parsing, you can try use localstorage