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
<{$smarty()}>
<{$smarty.session.username}>
<{$smarty.now}>
<{$smarty.const.ROOT}>
<{$smarty.const.M_PI}>
<{$smarty.current_dir}>