Home >Backend Development >PHP Tutorial >Smarty basic syntax and three major variables in php_PHP tutorial
Smarty templates are mainly based on variables. Here are the three major variables of Smarty and some common usages
First, create the files to be used in the root directory, which are basically the same as last time. configs is the configuration folder
1. Variables allocated from PHP
Use $smarty->assign()
$smarty->display()
First write the initialization php code, which is the same as conn.inc.php in the previous article
<!--?php include "init.inc.php"; //assign方法 $smarty--->assign("title","11111111"); $smarty->assign("content","2222222222"); //分配一个关联数组,用的较少 $smarty->assign(array("author"=>"何栘烽","time"=>date("Y-m-d H:i:s"))); //注册函数 $smarty->registerPlugin("function","myfun","test"); //注册插件,将test函数注册成myfun function test($args){ //args=array("size"=>7, "color"="yellow", "num"=>5, "connect"=>"3333333") //循环遍历出 $str=""; for($i=0; $i<$args["num"]; $i++){ $str.=''.$args["content"].' '; } return $str; } //数组 $smarty->assign("contacts",array("0575-1241243","kefeng.hyf@gmail.com", array("243214124", "45345"))); //对象 class Person{ public $name="kefeng"; function say(){ return $this->name."hhhhhhhh"; } } $smarty->assign("p",new Person()); $smarty->display("demo.html"); //显示的模板
2. Read variables from the configuration file
Here you need to create a configs folder with the configuration file test.conf
test.conf:
bodycolor=#3e3e3e bodybgcolor=yellow border=10 align=center width=800 bgcolor=gray [index] one=11111 [list] two=22222 [content] three=33333
<!--?php include="" smarty-=""-->display("demo.html"); //显示的模板 <!--?php-->
demo.html: Here write some header files and loading code
3. Reserved variables
Mainly include: $_GET
$_POST
$_SESSION
$_SERVER
$_ENV
1256b4c8edd9581360f57ad988f16334
a32dcd3f718e8dfc961d36181ffb6cb2
504e846c81a555eed2b9ff0768d0dcd6
bfa9c68c993788247c3bf060e36b4bf8
3cd23405a4ab0264b4dbddeeb0678e98
4a6ec2bdbba03bf520669b9b0f25db5d