Some analysis on the Smarty class
Pay special attention to the left and right separators <{}>,display------Display the contents of the template (inside the. html file), assign-------register the variable
<?PHP//is a key part of the Smarty class and parameter parsingclasssmarty{ Public $leftlimit= "<{";//Define left delimiter Public $rightlimit= "}>";//Define right delimiter Public $attr;//array for storing variable information//register variable functionAssign$k,$v) { $this->attr[$k] =$v; } //Show Templates functionDisplay$name) { //the path to creating a template $filename=$mubanlujing.$name;//index.html//Get template path $str=file_get_contents($filename); /**/ //use regular to match the contents of {} in the string//Read the contents (key inside the array to read), take the key to the array attr to take value//can be understood as two examples /**/ //put the files inside str into the cached content file_put_contents($filename,$str); //load the stored file to the current page include($filename); }}
Smarty Basics How to display content and type parameters
PHP page------main.php
<?PHP//Show on this pageinclude(".. /init.inc.php ");$name= "Zhang San";//can display a string$age= 20;//Show Numbers$attr=Array("China", "Shandong", "Zibo");//indexed Array$att=Array("One" = "Hello", "B" and "Hello");//Associative Arraysclassren{ Public $name= "Do not know"; Public $age;}$r=NewRen ();$r->age = "Do not know";//use key when uploading to an HTML page$smarty->assign ("Name",$name);//Show registered Variables$smarty->assign ("Age",$age);//Show Numbers$smarty->assign ("Dizhi",$attr);//Show index array, go to index number$smarty->assign ("Yuyan",$att);//can display associative arrays$smarty->assign ("Ren",$r);//Reading Objects$smarty->display ("main0603.html");//Display the contents of a template
HTML files in the templates
$name}></span></div><br/><div> age is: <{ the address of the $age}></div><br/><div> is: <{$dizhi[1]}></div><br/>< Div> language: <{$yuyan[' One ']}></div><br/><div> two kinds of wording the latter first: <{$yuyan. Two}></div><br/><div> His friend's name: <{$ren->name}></div><br/>< Div> Friend's age: <{$ren->age}></div></body>
Configuration file: init.inc.php
<?PHPDefine("ROOT",Str_replace("\\","/",dirname(__file__)).‘ /‘);//The root of the project is specified in the constant root//echo str_replace ("\ \", "/", DirName (__file__)). " /";requireROOT. ' Libs/smarty.class.php ';//loading the Smarty class file$smarty=NewSmarty ();//instantiate Smarty Object <br>//$smarty, auto_literal = false;//You can let the bounding symbol use spaces$smarty->settemplatedir (ROOT. ' Templates/');//set the location of all template files//$smarty->addtemplatedir (ROOT. ' Templates2/'); Add a template folder$smarty->setcompiledir (ROOT. ' Templates_c/');//set the directory where the compiled template resides$smarty->addpluginsdir (ROOT. ' Plugins/');//set as template extension to store directory$smarty->setcachedir (ROOT. ' cache/');//Set cache file storage directory$smarty->setconfigdir (ROOT. ' Configs/');//Setting the template configuration file to store directory$smarty->caching =false;//Setting the smarty cache switch function$smarty->cache_lifetime = 60*60*24;//set the cache template to a valid time of day$smarty->left_delimiter = ' <{';//set the left terminator in the template language$smarty->right_delimiter = '}> ';//set the right terminator in the template language
Smarty Register variables