Definition: Template variables
, which are variables assigned in the template, and how to use Smarty
rules to parse variables in the template.
In the Smarty
template, we divide the variables
in the template into three categories.
PHP assigns variables, that is, variables assigned using the assign
method.
Smarty retains variables, including super-global predefined variables
and smarty’s built-in variables.
Custom variables, users define variables in the template.
1. PHP allocates variables. In theory, PHP can allocate any data type to the template for parsing. Usually, there are only three types of data:
Scalar data: directly use the data output by the mark.
Array data: You can use subscripts in the smarty template or pass ".
" Subscript
.
Object data: In the smarty template, access is achieved through object accessor
.
<?php require 'smarty/Smarty.class.php'; $smarty=new Smarty(); // $smarty->left_delimiter="<{"; // $smarty->right_delimiter="}>"; $smarty->template_dir = 'templates/'; //实际模板所在目录,如果没有会在根目录下查找 //普通数据 $smarty->assign('hello',"hello world"); //数组 $smarty->assign('arr1',array(1412,14,23,456)); $smarty->assign('arr2',array('name'=>'张三','sex'=>'男')); //对象 class Person{ public $name='陈平安'; public $perr='saber'; } $smarty->assign('object1',new Person()); $smarty->display('model.html'); ?>
<!DOCTYPE html>//模板 model.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>
2.Smarty reserved variables: Smarty considers system variables or internal variables that users will need to use frequently. Such variables usually start with $smarty, followed by various keywords, which can be accessed multiple times.
GET data: {$smarty.get.name}
<html> <header></header> <body> GET数据:{$smarty.get.name} POST数据:{$smarty.post.name} session数据:{$smarty.session.username} cookie数据:{$smarty.cookies.username} REQUEST数据:{$smarty.request.name} server数据:{$smarty.server.SERVER_NAME} 时间戳:{$smarty.now} 模板路径:{$smarty.current_dir} 模板名字:{$smarty.template} </body> </html>
3. Custom variables : In order to flexibly process data in the template, Smarty allows setting variables: {assign var='variable name' value='variable value'}.
<html> <header></header> <body> {assign var='name' value='Sun'} {$name} </body> </html>
php tutorial,php video tutorial
The above is the detailed content of What are the data variables of smarty template and how to call them?. For more information, please follow other related articles on the PHP Chinese website!