PHP可以';不分配smarty变量

PHP可以';不分配smarty变量,php,smarty,Php,Smarty,我试图使用PHP在smarty中分配一些变量,但smarty变量返回NULL。这是我的密码: {php} $GLOBALS['smarty']->assign("new_var", "somevalue"); {/php} {$new_var|@var_dump} 上述代码将返回NULL 我不知道发生了什么。为什么不使用PHP文件 require("application/libraries/Smarty/libs/Smarty.class.php"); // Smarty class

我试图使用PHP在smarty中分配一些变量,但smarty变量返回NULL。这是我的密码:

{php}
$GLOBALS['smarty']->assign("new_var", "somevalue");
{/php}

{$new_var|@var_dump}
上述代码将返回
NULL

我不知道发生了什么。

为什么不使用PHP文件

require("application/libraries/Smarty/libs/Smarty.class.php"); // Smarty class Smarty                   
        $smarty = new Smarty();
        $smarty->assign('new_var' => $phpvar);
        $smarty->display('blabla.tpl');

试试这个语法{global put=foo value=$bar}并获取详细信息@Rahul我试过了,我的页面显示一个空白页面。我需要使用PHP来分配您在视图中分配它,这(大概)是在所有分配的变量都转换为
$vars
之后,为什么不直接执行
$new_var='somevalue'在php块中?@apokryfos,因为我需要在JavaScript中使用它。我可以像
var js_var='{php}echo$new_var;{/php}'?如果用简单的变量声明替换共享代码中的
{php}$GLOBALS…{/php}
部分,您可以继续在视图中正常使用
$new\u var