Smarty3 configuration and entry syntax, smarty3 entry syntax
1. Smarty3 Configuration
Download the Smarty File
Download the Smarty file from the Smarty official website, decompress the downloaded Smarty file, and the Smarty library file is in the libs folder.
The integration package of the PHP debugging environment I use is phpstudy. By default, there is a WWW folder on disk D. Create a folder named testSmarty In the WWW folder, the testSmarty folder is the project root directory of the Smarty exercise. Create a smarty folder in testSmarty and copy the files in libs to this smarty folder. The directory structure is as follows:
Smarty Configuration
Create a test folder in the testSmarty folder and a config. php file in this folder. The content is as follows:
<? Phpheader ("content-type: text/html; charset = UTF-8"); // introduce the core file and instantiate require ('/WWW/testSmarty/smarty/Smarty. class. php '); $ smarty = new Smarty (); // configure // template file path $ smarty-> template_dir ='/WWW/testSmarty/test/tpl '; // path of the template file after compilation $ smarty-> compile_dir = '/WWW/testSmarty/test/template_c '; // buffer file path $ smarty-> cache_dir = '/WWW/testSmarty/test/cache'; // enable the buffer, buffer is disabled by default $ smarty-> caching = true; // cache retention time $ smarty-> cache_life Time = 120;?>
When setting a path, I use absolute or relative paths. Create the tpl, template_c, and cache folders in the test folder Based on the set path.
The directory is as follows:
Check whether the smarty configuration is successful:
Create a start. php file in the test folder. The file content is as follows:
<? Php // introduce the configuration file require ('config. php '); // pass the value $ smarty-> assign ('text', 'Hello Smarty') to the template file '); // rendering template $ smarty-> display ('Start. tpl ');?>
Create a start. tpl file in the tpl file with the following content:
{$text}
The template file suffix does not have to be tpl
Enter localhost: 800/testSmarty/test/start. php In the browser
Note:In my case, Apache is a port 800.
If hello Smarty is displayed in the browser, the Smarty configuration is successful.
Ii. Entry syntax
Identifier
Smarty labels are located between delimiters. The default delimiters are {And}. The delimiters can be changed. Modify the delimiter in the configuration file as follows:
// Change the left delimiter #
$ Smarty-> left_delimiter = '#';
// Change the right definer #
$ Smarty-> right_delimiter = '#';
When Smarty reads the content between the delimiter, it tries to parse it and output the result. Content other than the identifier is static.
Note
The comment ends with "*" and contains a separator, as shown below:
{* Here is the comment *}
Output variable
// Pass the value $ smarty-> assign ('variable name', value) to the template to be rendered)
The passed value can be a string or an array or an object.
When the variable is a string, {$ variable name} indicates the output variable.
When the variable is an array, there are two ways to output the variable:
A. {$ arr. key}
B. {$ arr ['key']}
When it is an object, you can directly call the object method through obj-> method ().
Condition judgment
The basic sentence for condition determination is as follows:
{If $ variable Condition modifier value1} // some {elseif $ variable Condition modifier value2} // some {else} // some {/if}
Basic Condition modifiers include: eq (equal to), neq (not equal to), gt (greater than), lt (less than), lte (less than or equal to), and gte (greater than or equal ). There must be spaces before and after the Modifier
Loop
There are two cycles in Smarty: foreach, section, for, and while.
Section Loop
Section cannot traverse the associated array cyclically. It can only traverse the arrays of consecutive numbers.
Section can accept name, loop, step, start, max, show, where name and loop are required. Name is the subscript of each loop, and loop is the value of a loop. The step of a step loop is 1 by default, max is the maximum number of cycles, and start starts the subscript of the loop, the default value is 0. If start is negative, the loop starts from the end of the array. show indicates whether the loop content is displayed. The default value is true.
As follows:
Create the section. php file in the test folder. The content is as follows:
<? Phprequire ('config. php '); $ article = array ('title' => 'Chapter 1', 'name' => 'qxqstar ', 'content' => 'smarty section loop Chapter 1 '), array ('title' => 'Chapter 2', 'name' => 'qxqstar ', 'content' => 'smarty section loop Chapter 2 '); $ Smarty-> assign ('Article', $ article); $ smarty-> display ('section. tpl ');?>
Create the section. tpl file in the tpl folder. The content is as follows:
<Ul> {section name = item loop = $ article} <li >{$ article [item]. title} <p> author: {$ article [item]. name} </p> <p> content: {$ article [item]. content} </p> </li> {sectionelse} No article {/section} </ul>
Foreach Loop
The foreach loop is simpler than the section loop. The foreach syntax is similar to the foreach syntax in php. foreach can implement any section function, and foreach is simpler.
{foreach $arrayvar as $itemvar}{foreach $arrayvar as $keyvar=>$itemvar}
Example:
Create the foreach. php file in the test folder. The file content is as follows:
<? Phprequire ('config. php '); $ articles = array ('title' => 'Chapter 1', 'name' => 'qxqstar ', 'content' => 'smarty foreach loop Chapter 1 '), array ('title' => 'Chapter 2', 'name' => 'qxqstar ', 'content' => 'smarty foreach loop Chapter 2 '); $ Smarty-> assign ('articles', $ articles); $ smarty-> display ('foreach. tpl ');?>
Create the foreach. tpl file in the tpl folder. The content is as follows:
<Ul> {foreach $ articles as $ article} <li >{$ article. title} <p >{$ article. name} </p> <p> {$ article. content} </p> </li> {foreachelse} <p> no article </p> {/foreach} </ul>
For Loop
The for loop is used to create a simple loop. The syntax is as follows:
{for $variable = $startValue to $entValue step $step}
{For $ I = 0 to 10 step 2} {$ I} {foreach} cannot carry out loops {/}
The step can be left unspecified. The default cycle step is 1. For can also specify a max attribute, which is used to specify the number of cycles
Similar to a foreach loop, A for loop also has a forelse. {Forelse} is executed when the loop cannot be traversed.
While Loop
The while loop of Smarty is similar to the while loop in php. The syntax is as follows:
{while $variable > 0}//some{/while}
You can use {break} in the loop to stop the loop and {continue} to exit the current loop and enter the next loop.
Plug-ins
The essence of the Smarty plug-in is the function. There are three types of plug-ins: function plug-ins, modifier plug-ins (variable adjustment plug-ins), and block plug-ins.
Use plug-ins
Function plug-in usage: {funcName param1 = value ...}
Use of the modifier plug-in: {$ variable | modifierName: param1 ...}
Use of block plug-ins:
{blockName param1 = value...} {$content}{/blockName}
For example:
// Function plug-in use {include file = 'header. tpl '} // use of the modifier plug-in {'abc' | cat: 'D'} // use of the block plug-in {textformat wrap = 10} abc defghikj lmn {/textformat}
Create plug-ins
There are three ways to create plug-ins
1. Use the registerPlugin function to register a UDF.
2. Place the completed plug-ins in the Plugins directory in the smarty directory.
3. PHP built-in functions can be automatically used in the template in the form of modifier plug-ins (variable adjustment plug-ins)
Use the registerPlugin function to register the custom plug-in. The example is as follows:
Create the defineFunc. php file in the test folder. The content is as follows:
<?phprequire ('config.php');function test($params){ $p1 = $params['p1']; $p2 = $params['p2']; return $p1.$p2;}$smarty->registerPlugin('function','f_test','test');$smarty->display('definedFunc.tpl');?>
Create a defineFunc. tpl file in the tpl folder. The content is as follows:
{f_test p1='learn' p2=' Smarty'}
The first parameter of the registerPlugin function indicates the type of the registered program. Here, the function indicates that the registered function plug-in is used. You can also obtain block and modifier values, the second parameter refers to the registered Smarty function plug-in name, and the third parameter refers to the custom function name. The second parameter can be the same as the third parameter.
When {f_test p1 = 'learn' p2 = 'smarty '} is executed in Smarty, p1 and p2 are packaged into an array and passed to our defined test function.
Create a plug-in by adding files to the Plugins folder.
1. Create a function plug-in
Create a function. test. php file in the Plugins folder (function indicates the function plug-in, and test indicates the plug-in name). The content is as follows:
Function smarty_function_test ($ params) {// $ params is an associated array $ w = $ params ['W']; $ h = $ params ['H']; return $ w * $ h;}?>
Directly use the test function plug-in the template
{test h = 3 w = 5}
These two parameters are packaged into an array and passed to the smarty_function_test function.
2. Create the modifier plug-in
Create a modifier. test2.php file in the Plugins folder (modifier indicates this modifier plug-in, and test2 indicates the plug-in name). The content is as follows:
function smarty_modifier_test2($utime,$formate){ return date($formate,$utime);}
Use the test2 modifier plug-in the template
{$time|test2:'Y-m-d H-i-s'}
The modifier plug-in and function plug-in are different. The function plug-in's custom function only receives one array parameter. The modifier plug-in's custom function
List the received parameters one by one
3. Create a block plug-in
Create a block. test3.php file in the Plugins folder (block indicates this block plug-in, and test3 indicates the plug-in name). The content is as follows:
function smarty_block_test3($params ,$content){ $replace = $params['replace']; $maxnum = $params['maxnum']; if($replace == 'true'){ str_replace(',', ',', $content); str_replace('。', '.', $content); } $content = substr($content,0,$maxnum); return $content;}
Use the test3 block plug-in the template
{test3 replace='true' maxnum = 6}abcdfedddderere{/test3}
Content Between {test3} {/test3} is transmitted to the second parameter in smarty_block_test3, and the parameters followed by test3 are packaged into an array and passed to the first parameter in smarty_block_test3.
The above is all the content of this article. I hope this article will help you in your study or work. I also hope to provide more support to the customer's home!