$ TPL = new smarty (); // create a new smarty object. What I use isSmarty-1, 3.1.6Version
1. Set the path to the smarty template $ TPL->Settemplatedir ()By defaultTemplates
2. Set the path for compiling the smarty template $ TPL->Setcompiledir ()By defaultTemplates_c
3. Set the Left and Right Separators of the smarty template engine,
$ TPL-> left_delimiter = "<{";
$ TPL-> right_delimiter = "}> ";
By default: Public $ left_delimiter = "{"; // smartySource code
Public $ right_delimiter = "}"; // smarty SourceCode
Why should we change these delimiters?
For example, in an earlier version of the smarty engine template, an error is reported and cannot be identified automatically.
For example: <style> Div {margin: 0 ;}</style> or <SCRIPT> function show () {alert ("Smarty") ;}</SCRIPT>
In both cases, there are "Left and Right braces", and an error will be reported when the smarty engine encounters
4. For initialization, we can create another PHP file for initialization, such as smarty. ini. php. Then include it in the PHP file.
<? PHP
Include"../Smarty3.1.6/libs/smarty. Class. php ";
$ TPL=NewSmarty ();
$ TPL-> Settemplatedir ("./TPL ");
$ TPL-> Settemplatedir ("./compile ");
$ TPL-> Left_delimiter = "<{";
$ TPL-> Right_delimiter = "}> ";
?>
5. When using the display function or include other templates of the smarty template engineThe template directory specified in the smarty object (for example, the TPL Directory, which defaults to the templates directory) is used as the base directory..
① The template directory is TPL, which stores many templates, including default, green, red, and default templates. There are many template files (index. TPL, header. TPL, footer. TPL), the correct usage of display at this time: $ TPL-> display ("default/index. TPL); that is, the default template directory under the base Directory
② In the template file (such as index. TPL) contains other template files (such as header. TPL, footer. TPL), the correct format of include should be: <{include "default/header. TPL "}>,< {include" default/footer. TPL "}>
Although index. TPL, header. TPL, footer. TPL is in the same directory, but <{include "header. TPL "}>, <{include" footer. TPL "}> is incorrect syntax. In this case, the smarty engine will find the header and footer in the TPL directory, instead of the default directory.
6. If you want to make PHP under various directoriesProgramYou can load smarty and use the template directory specified by smarty and the compilation directory. The only way is to useAbsolute path.
7. How to access variables in the smarty template engine (Add "$" before the variables in the template"Symbol)
① Access an array
$ TPL-> assign ("arr", array ("AA", "BB", "cc "));
$ TPL-> assign ("arr2", array (Array ("two-dimensional arrays one by one", "two-dimensional arrays one or two"), array ("two-dimensional arrays one by one ", "Two-dimensional array 2 ")));
Access Index Array: <{$ arr [0]}>, <{$ arr [0]}>, <{$ arr [0]}>
Access the two-dimensional Index Array: <{$ arr2 [0] [0] }>,< {$ arr2 [0] [1]}>
- Join array: (Use.Symbol to access)
Access associated array: <{$ arr3.id} >,< {$ arr3.name} >,< {$ arr3.age}>
② Access Object
Class Human {
Private $ Sex ;
Private $ Name ;
Private $ Age ;
Public Function _ Construct ( $ S , $ N , $ ){
$ This -> Sex = $ S ;
$ This -> Name = $ N ;
$ This -> Age = $ ;
}
Public Function Print_info (){
Return $ This -> Sex ."--". $ This -> Name ."--". $ This -> Age;
}
}
$ TPL -> Assign ("student ", New Human ("male", "marcofly", 22 ));
Assign values to objects in the template: <{ $ Student -> Print_info ()}>
8. mathematical operations in the smarty template engine can be applied to template variables.
- Assign values to variables
$ TPL-> assign ("num1", 10 );
$ TPL-> assign ("num2", 5.5 );
<{$ Num1}> // result 10
<{$ Num2}> // Result 5.5
<{$ Num1 + $ num2}> // result 15.5
<{$ Num1 + $ num2 * $ num2/$ num1}> // result 13.025
9. when using images, CSS files, and JS files in a template file, the path must be the PHP file to be accessed (such as; index. PHP), because we access the PHP file, and the template file (such as index. TPL) is included in the PHP File
- Article from Web development _ Xiaofei
- reprinted Please note: http://www.cnblogs.com/hongfei/archive/2011/12/08/smarty_attention.html