I'm trying to define a JS variable which gets it's value from a smarty variable..
This is what I've done in my PHP controller:
public function hookDisplayBackOfficeHeader()
{
$this->context->controller->addJS($this->_path.'js/bo_setup.js', 'all');
}
I'm declaring the variable on a separate function:
private function _loadTestInfo()
{
$this->context->smarty->assign(array(
'test_username' => 'myuser',
));
}
and calling it from the getContent() function:
{
$output = '';
....
$this->output .= $this->display(__FILE__, '/views/templates/admin/back_office.tpl');
$this->_loadTestInfo();
$this->output .= $this->renderForm();
return $this->output;
}
my bo_setup.js function looks like this:
var test_username = "{$test_username}";
document.getElementById('username').value = test_username;
However, running the page gives the 'username' variable the value of "{$test_username}" instead of the "myuser" value.
any clues?
For accessing variables in JavaScript you can assign them in your controllers with:
Media::addJsDef(array('mymodule' => array('test_username' => 'Your name')));
$this->context->controller->addJS($this->_path.'myscript.js')
Then you can use them in your JavaScript or through console:
let var1 = mymodule.test_username;