我正在尝试定义一个 JS 变量,它从一个 smarty 变量中获取它的值..
这是我在我的 PHP 控制器中所做的:
public function hookDisplayBackOfficeHeader()
{
$this->context->controller->addJS($this->_path.'js/bo_setup.js', 'all');
}
我在一个单独的函数上声明变量:
private function _loadTestInfo()
{
$this->context->smarty->assign(array(
'test_username' => 'myuser',
));
}
并从 getContent() 函数调用它:
{
$output = '';
....
$this->output .= $this->display(__FILE__, '/views/templates/admin/back_office.tpl');
$this->_loadTestInfo();
$this->output .= $this->renderForm();
return $this->output;
}
我的 bo_setup.js 函数如下所示:
var test_username = "{$test_username}";
document.getElementById('username').value = test_username;
但是,运行该页面会为“用户名”变量提供“{$test_username}”的值,而不是“myuser”的值。
任何线索?