<< Back to man.ChinaUnix.net

Chapter 4. Variables[第四章.变量]

Table of Contents[内容列表]
Variables assigned from PHP[从PHP分配的变量]
Variables loaded from config files[从配置文件读取的变量]
{$smarty} reserved variable[{$smarty} 保留变量]

Smarty has several different types of variables.
The type of the variable depends on what symbol it is prefixed with (or enclosed within).

变量 的类型取决于它的前缀是什么符号(或者被什么符号包围)

Variables in Smarty can be either displayed directly or used as arguments for function attributes and modifiers, inside conditional expressions, etc.
To print a variable, simply enclose it in the delimiters so that it is the only thing contained between them. Examples:



<body bgcolor="{#bgcolor#}">

Variables assigned from PHP

Table of Contents[内容列表]
Associative arrays[关联数组]
Array indexes[数组下标]

Variables that are assigned from PHP are referenced by preceding them with a dollar sign $.
Variables assigned from within the template with the assign function are also displayed this way.


Example 4-1. assigned variables
例 4-1.分配的变量


$smarty = new Smarty;
$smarty->assign('firstname', 'Doug'); $smarty->assign('lastLoginDate', 'January 11th, 2001'); $smarty->display('index.tpl');
index.tpl: Hello {$firstname}, glad to see you could make it. <p> Your last login was on {$lastLoginDate}. OUTPUT: Hello Doug, glad to see you could make it. <p> Your last login was on January 11th, 2001.