我们可以在 .tpl 文件中定义变量吗?

Can we define variables in `.tpl` files?

一般来说,如果我们有一个 Smarty 项目,我们会在 .php 文件中分配变量,并在其对应的 .tpl` 文件中使用变量。

如:

$smarty->assign('foo', 'Foo');  // in .php file

{$foo}  // in .tpl file

我们可以在.tpl中定义变量吗?如果可以,这是推荐吗? 为什么?

是的,您可以:

{assign var="foo" value="Foo"}
{assign "foo" "Foo"} {* short-hand *}

The value of $foo is Foo.

通过调用 $ss->assign('i', $i); 巧妙地使用,在您的 .tpl 文件中您可以使用 {$i} 符号访问它。