Include ("Smarty.class.php"); $smarty = new Smarty; $smarty->caching = true;
Only do DB calls if cache doesn ' t exist Call the database only if the cache does not exist if (! $smarty->is_cached ("Index.tpl")) {
Dummy up some data $address = "245 N 50th"; $db _data = Array ( "City" = "Lincoln", "state" = "Nebraska", "Zip" = > "68502" );
$smarty->assign ("Name", "Fred"); $smarty->assign ("Address", $address); $smarty->assign ($db _data);
}
Display the output Display output $smarty->display ("Index.tpl");3.The TPL variable adjusts the output:①{$var |capitalize}//Capitalize first letter②{$var |cat: ' String '}//string connector (: equivalent.){$var |cat: ' string1 ': ' string2 '}//to output multiple strings simply: ' Srting '③{$time |date_format: "%h:%m:%s"}//date_format (Time,format) and date (Format,time)④{$var |default: ' content '}//Specify the default value of $var as content (regardless of whether Var has a value)⑤{$var |escape: ' url '}//to transcode var to URL mode (specify transcoding mode in the following ")⑥{$url |lower} {$url |upper}//Before all the variables are lowercase and all the following are uppercase⑦{$var |NL2BR}//Because the HTML does not recognize the newline character, this statement converts the newline character in the variable string to <br/>Example://test.php$smarty->assign (' score ', ' 91 '); Test.tpl{if $score GT 90} excellent {ElseIf $score GT 60} qualifying {Else} failed {/if} looping mode//test.php $articlelist =array ( Array ("title" = "First article", "Author" = "Little J", "content" = "First article memory"), Array ("title" = "The second article", "Auth or "=" + "small L", "content" = "second article memory"); $smarty->assign (' articlelist ', $articlelist); $smarty->display (' Test.tpl '); Test.tpl Loop Method One: {section name=article loop= $articlelist} {$articlelist [article].title} {$articlelist [Artic Le].author} {$articlelist [article].content}<br/>{/section} SectionPropertiesName:(required) is a Sectionthe name of the loop is just the name of the loop. No special meaning, no previous $symbols;Loop: (required) is inPHPthe name of the variable in the declaration to indicate which array (that is, the array name to loop) needs to be used $;Start: (optional) The initial position of the loop execution . If the value is negative, the start position is calculated from the end of the array . For example, if the array has7element, specifying a Startto be-2 , the index that points to the current array is 5. Illegal value( The lower bound of the loop array is exceeded )will be automatically adjusted to the nearest legal value .Step: (optional) such as other languages of the loop, is a step, if negative, then the reverse loop;Max:(optional) The maximum index of the loop, if it is 1the loop only1 times, if for 2the Loop2times;Show: (optional) defaults totrueis displayed. If you set the {Sectionelse}. Indicates that the contents of this section are displayed if the array has no content; Showto befalse This section is displayed. If not set {Sectionelse}the array is not output. Loop method Two: {foreach $articlelist as $article} {$article. title} {$article. author} {$article. content}<br/ >{/foreach} smarty Reference ①{include file= ' HEADER.TPL '}//output HEADER.TPL file contents ②{include file= ' header.tpl ' sitename= ' Content '}//content instead of {$stitename} Smarty Class and object assignment in the Header.tpl file/ * The class that is defined and instantiated in test.php, passed to Test.tpl, can be directly used directly on the method of the class */Test.php class my_object{function Meth1 ($pam) {return $pam [0]. ' A '. $pam [1]; }} $my _obj=new my_object (); $smarty->assign (' my_obj ', $my _obj); $smarty->display (' Test.tpl '); test.tpl{$my _obj->meth1 (' You are ', ' good guy ')}//Web output: You're a good man. Use of Smarty functionsExample 1: Test.php$smarty->assign (' Time ', Time ()); $smarty->display (' Test.tpl ');/* For function use, | Before as the first parameter of the DATE function: After as the second argument */test.tpl{"y-m-d" |date: $time}//output:2016-03-10Example 2:/*php function Str_replace ($str 1, $str 2, $STR) where the first parameter in the string str is replaced by the second parameter */Test.php$smarty->assign (' str ', ' asdfgh '); $smarty->display (' Test.tpl '); test.tpl{' F ' |str_replace: ' K ': $str}//replace F in str with KExample 3://test.phpfunction test ($params) {$p 1= $params [' P1 ']; $p 2= $params [' P2 ']; The value of return ' entered key value 1 is: '. $p 1. ', the value of the key value 2 entered is: '. $p 2; }//Register function plug-in type is function, define plug-in function named F_test,test as callback function name$smarty->registerplugin (' function ', ' f_test ', ' test '); $smarty->display (' Test.tpl '); Test.tpl{f_test p1= ' hhhh ' p2= ' KKKK '}//Adds an arrayArray ([p1] = hhhh [P2] = KKKK) callback to test.php in the parameters of the test function $paramsOutputThe value of the key value 1 that is entered is: HHHH, the value of the key value 2 that is worn is: kkkk mvc:c through M to obtain data, C again through V will get the data output, where m is the business logic layer is the most core part, if found their C layer code more and more may not well distinguish with M division of LaborExample 1://function function Plug-inFunction.test.phpfunction Smarty_function_test ($params) {//smarty_function_ Plug-in name (equivalent to C)$width = $params [' width ']; $height = $params [' height ']; $area = $width * $height; return $area; }//test.php $smarty->display (' Area.tpl '); //output:20000 (equivalent to V)Area.tpl{test width=100 height=200}//$params gets an array of AREA.TPL callbacks (equivalent to M) Example 2://modifiers variable adjuster plug-in//modifier.test.php function Smarty_modifier_test ($utime, $format) {return date ($format, $utime); PHP's date (format, timestamp)}//test.php $smarty->assign (' Time ', hour ());//Get local timestamp$smarty->display (' Utime.tpl ');//Outpututime.tpl{$time |test: ' y-m-d h:i:s '}//$time is passed as the first variable to $utime,' y-m-d h:i:s ' to $format (difference: function returns an array) Example 3://block functions block function plug-in (can pass an array like function and can pass parameters like modifier) with closed flags the above hints indicate that the block re-definition test function conflicts with the function definition test, so the following definition test1 //block.test1.phpfunction Smarty_block_test1 ($params, $content) {//$replace = $params [' Replace ']; $maxnum = $params [' Maxnum ']; if ($replace = = ' true ') {$content =str_replace (', ', ', ', $content); $content =str_replace ('. ', '. ', $content); } $str =substr ($content, 0, $maxnum); return $str; }//test.php$smarty->assign (' str ', ' Hello World,hello man and Women,hell dog and Pig. '); $smarty->display (' Block.tpl '); Block.tpl{test1 replace= ' true ' maxnum=200}//pass array to $params{$STR}//$str Pass the value to the $content{/test1}//Note: There is a closed sign hereExample 4://org customization of third-party class libraries//function.php function C ($name, $method) {//Instantiate controller functionsRequire_once ('/libs/contorller/'. $name. ' Controller.class.php '); $controller = $name. ' Controller '; $obj =new $controller (); $obj $method (); }//Custom third-party class library function ORG ($path, $name, $params =array ()) {//path is a path, name is a third-party class name//params is the property of the class that needs to be specified and assigned when the class is initialized, Format is array (property name = property value ...) Require_once (' libs/org/'. $path. $name. Class.php '); ORG is a third-party plug-in class library $obj =new $name (); if (!empty ($params)) {foreach ($params as $key = = $value) {$obj $key = $value; Instantiate object property Value}} return $obj; }//config.php//Smarty configuration is specifically placed in the configuration file$viewconfig =array (' left_delimiter ' = ' {', ' right_delimiter ' = '} ', ' template_dir ' = ' tpl ', ' compile_dir ' = > ' Template_c ');//index.phpRequire_once (' function.php '); Require_once (' config.php '); $view =org (' smarty/', ' Smarty ', $viewconfig); Pass Smarty's third-party class library and Smarty class and Smarty configuration variables to the org function $controller =$_get[' controller '; $method =$_get[' method ']; C ($controller, $method);//testcontroller.class.phpClass testcontroller{function Show () {global $view; $view->assign (' str ', ' haha-ha '); $view->display (' Test.tpl '); } }//TEST.TPL{$STR}//output: haha haha |