Understanding Smarty Template Variables: Assignment, Reserved, and Custom Variables
This article explains the three kinds of Smarty template variables—PHP‑assigned, reserved, and custom—illustrates how to assign scalar, array, and object data from PHP, lists common reserved variables, and shows how to define variables directly in templates with code examples.
In Smarty templates, a template variable is a variable assigned to the template and accessed according to Smarty's parsing rules.
Smarty categorises template variables into three groups:
PHP‑assigned variables, created with the assign method.
Reserved Smarty variables, which include super‑global predefined variables and built‑in Smarty variables.
Custom variables, defined directly inside the template.
1. PHP‑assigned variables – PHP can assign any data type to the template, typically scalar, array, or object data.
Scalar data: output directly.
Array data: accessed via index notation (e.g., {$arr1[0]} ) or dot notation (e.g., {$arr1.0} ).
Object data: accessed using the object operator (e.g., {$object1->name} ).
<code><?php
require 'smarty/Smarty.class.php';
$smarty = new Smarty();
// $smarty->left_delimiter = "<{";
// $smarty->right_delimiter = "}>";
$smarty->template_dir = 'templates/'; // actual template directory
// scalar
$smarty->assign('hello', "hello world");
// array
$smarty->assign('arr1', array(1412, 14, 23, 456));
$smarty->assign('arr2', array('name'=>'张三', 'sex'=>'男'));
// object
class Person {
public $name = '陈平安';
public $perr = 'saber';
}
$smarty->assign('object1', new Person());
$smarty->display('model.html');
?>
</code> <code><!DOCTYPE html>
<html>
<head><title></title></head>
<body>
{$hello} 这是templates下面的模板 <br>
这是索引数组:{$arr1[0]}---{$arr1[1]}---{$arr1[2]} <br>
这是索引数组:{$arr1.0}---{$arr1.1}---{$arr1.2} <br>
这是关联数组:{$arr2.name}---{$arr2.sex} <br>
这是对象:{$object1->name}---{$object1->perr} <br>
</body>
</html>
</code>2. Reserved Smarty variables – These are system or internal variables that start with $smarty and are frequently used, such as:
GET data: {$smarty.get.name}
POST data: {$smarty.post.name}
Session data: {$smarty.session.username}
Cookie data: {$smarty.cookies.username}
REQUEST data: {$smarty.request.name}
Server data: {$smarty.server.SERVER_NAME}
Timestamp: {$smarty.now}
Current template directory: {$smarty.current_dir}
Template name: {$smarty.template}
Config values: {$smarty.config.key}
<code><html>
<header></header>
<body>
GET data: {$smarty.get.name}<br/>
POST data: {$smarty.post.name}<br/>
Session data: {$smarty.session.username}<br/>
Cookie data: {$smarty.cookies.username}<br/>
REQUEST data: {$smarty.request.name}<br/>
Server data: {$smarty.server.SERVER_NAME}<br/>
Timestamp: {$smarty.now}<br/>
Template path: {$smarty.current_dir}<br/>
Template name: {$smarty.template}
</body>
</html>
</code>3. Custom variables – Smarty allows defining variables directly in the template using the {assign} tag, for example:
<code><html>
<header></header>
<body>
{assign var='name' value='Sun'}
{$name}
</body>
</html>
</code>The article concludes with a link to the original source for further reading.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.