PHP smarty Basic syntax and three major variables
In the Smarty template is mainly based on variables, the following is about smarty three variables and some common usage
First build the file you want to use in the root directory the same as the last time, Configs is the configuration folder
One, the variables allocated from PHP
With $smarty->assign ()
$smarty->display ()
First write the initial PHP code, and the same as the previous article conn.inc.php
Assign ("title", "11111111"); $smarty->assign ("Content", "2222222222"); Allocates an associative array with less $smarty->assign ("Author" = "He Hing", "Time" =>date ("y-m-d h:i:s")); The registration function $smarty->registerplugin ("function", "myfun", "Test"); Register the plug-in and register the test function as Myfun function Test ($args) { //args=array ("Size" =>7, "color" = "yellow", "num" =>5, " Connect "=" 3333333 ") //Loop through $str =" "; for ($i =0; $i < $args ["num"]; $i + +) { $str. = '. $args ["Content"]. '
'; } return $str; } Array $smarty->assign ("Contacts", Array ("0575-1241243", "[email protected]", Array ("243214124", "45345"))); Object class person{public $name = "Kefeng"; function say () { return $this->name. " Hhhhhhhh "; } } $smarty->assign ("P", new Person ()); $smarty->display ("demo.html"); The template that is displayed
Demo.html:
<{$title}> content:<{$content}>
content:<{$content}>
content:<{$content}>
author:<{$author}>
time:<{$time}>
Time:<{myfun ()}>
Time:<{date ("y-m-d")}>
<{myfun size= "7" color= "Yellow" num= "5" content= "3333333"}><{$contacts [0]}>
<{$contacts [2][0]}>
<{$p->name}>
<{$p->say ()}>
Second, read the variable from the configuration file
Here you need to create a configs folder, inside the configuration file test.conf
Test.conf:
bodycolor= #3e3e3ebodybgcolor =yellowborder=10align=centerwidth=800bgcolor=gray[index]one=11111[list]two=22222[ content]three=33333
Open session in Demo.php
Display ("demo.html"); The template that is displayed
Note that the following are:
load config file: <{config_load "test.conf"}>
READ: <{#内容 #}>
Loading a zone file takes the second parameter: <{config_load "test.conf" section= "index"}>
Demo.html: In, here to write some header files, as well as loading code
<{$title}> <{config_load "test.conf" section= "index"}> .... ....... <{#one #}>
<{#two #}>
<{#three #}>
......
Three, 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}>