【问题标题】:Use the smarty variable in smarty project php file在 smarty 项目 php 文件中使用 smarty 变量
【发布时间】:2018-07-18 10:51:01
【问题描述】:

我有一个 smarty 项目,在 php 文件中,我设置了一个 abc 变量:

$smarty->assign('abc', '123');

现在我可以在其对应的.tpl 文件中使用{$abc}

但是现在我可以在 php 文件中使用abc 吗?

我试过$smarty->abc$smarty->$abc都无法访问。


EDIT-01

如果我使用

$smarty->assign('abc', '1234');
$abc2 = $smarty->get_template_vars('abc');

我会得到以下错误:

Fatal error: Uncaught Error: Using $this when not in object context in /Users/sof3/Desktop/smarty-test02/php/test.php:36 Stack trace: #0 {main} thrown in /Users/sof3/Desktop/smarty-test02/php/test.php on line 36

【问题讨论】:

  • 您可以使用get_template_vars() 函数并像这样访问变量$abc = $this->get_template_vars('abc');
  • @Bhavin 兄弟,看看我的 eidt-01。
  • 你有assign()-ed 东西给$this 吗?我不这么认为。您可以通过调用$smarty 对象的assign() 方法让Smarty 知道abc。使用$smarty->get_template_vars('abc') 取回值。
  • 致命错误:未捕获 --> Smarty:未定义扩展类 'Smarty_Internal_Method_Get_Template_Vars'
  • 是的,sof-03。 @axiac 是对的 这是我的错字使用 $smarty->get_template_vars('abc') 像这样。

标签: php smarty


【解决方案1】:

如果您的 Smarty 版本是第 2 版。您可以使用:

$smarty->get_template_vars('abc')

但如果你的 Smarty 版本是 3,你应该使用:

$smarty->getTemplateVars('abc') 

更多详情请参考:https://www.smarty.net/docs/zh_CN/language.syntax.functions.tpl

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-18
    • 2013-04-17
    • 1970-01-01
    相关资源
    最近更新 更多