: This article mainly introduces -------- smarty. if you are interested in the PHP Tutorial, refer to it. Smarty
Day01 ----- usage of smarty
Function: separates php and html, programs, and artists.
Usage:
1. download the source code package
2. directory structure
Libs
Source code file
Smarty. class. php
Template // template file html file
Template_c // The template compilation file automatically generated during the program running
3. instantiation
Require "Smarty. class. php ";
$ SmartyOb = new Smarty ();
4. configuration
Tell Smarty which Directory contains the Template html
$ SmartyOb-> setTemplateDir (path );
Tell smarty which Directory stores the compiled files
$ SmartyOb-> setCompileDir (path)
5. display template
$ SmartyOb-> display ("template name and path ");
How to Pass values to a template
$ SmartyOb-> assign ('template name', passed variable );
How to call a template
Template tag
{$ NAME}
Array
{$ Array name. subscript}
Two-dimensional array
Array (
0 => array ('title' => 'T1', 'pubtime' => 1234567890, 'author' => 'Xiaoli '),
1 => array ('title' => 'T2', 'pubtime' => 1334567890, 'author' => 'Xiaoli '),
Array ('title' => 't3 ', 'pubtime' => 1434567890, 'author' => 'Xiaoli '),
Array ('title' => 'T4 ', 'pubtime' => 1534567890, 'author' => 'Xiaoli '),
)
Traverse arrays
{Foreach array name as variable name}
{$ Variable name. title}
{/Foreach}
Tag value
<{$ Variable name @ key}> <{$ variable name @ index}>
Number of cycles
<{$ Variable name @ total}>
The number of cycles currently
<{$ Variable name @ iteration}>
Is it the first cycle?
<{$ Variable name @ first}>
Is it the last loop?
<{$ Variable name @ last}>
Case:
Implement a background color change
<{If condition}>
Code
<{/If}>
Exercise:
Underline each li (dotted line), except the last line
Knowledge:
<{Section name = s1 loop = traversed array}>
<{$ Array [s1]. subscript}>
<{/Section}>
Attribute:
<{$ Smarty. section. s1.total}>
<{$ Smarty. section. s1.index}>
<{$ Smarty. section. s1.iteration}>
<{$ Smarty. section. s1.last}>
<{$ Smarty. section. s1.first}>
Exercise:
Use section to achieve different background colors
Variable regulator
Template tag delimiter:
<{}>
$ SmartyOb-> left_delimiter = "<{";
$ SmartyOb-> right_delimiter = "}> ";
Day02 ---------: review the content of day01 and expand new knowledge
Smarty
1. put Smarty in the main php program
2. configuration
$ SmartyOb-> setTemplateDir ()
$ SmartyOb-> setCompileDir ()
$ SmartyOb-> left_delimiter ()
$ SmartyOb-> right_delimiter ()
3. pass a value to the template
$ SmartyOb-> assign ("template variable name", variable );
4. display template
$ SmartyOb-> display (path );
{$ NAME}
{$ Array. subscript}
{Foreach array name as variable}
{$ Variable name}
{/Foreach}
Tag attributes:
{$ Variable name @ key}
{$ Variable name @ index}
{$ Variable name @ iteration}
{$ Variable name @ total}
{$ Variable name @ first}
{$ Variable name @ last}
{If condition}
{Else}
{/If}
Variable regulator:
{First parameter | function name: Second parameter :.......}
Article Details page:
02_detail.php? Id = 5
1. configure smarty
2. received Article id
3. instantiate pdo
4. spell SQL statements
5. execute
6. get data
7. pass a value to the template
8. display template
9. set the template tag and use the data transmitted by the main program
Page splitting
{Include file = "template path "}
Register a function
Function fun1 (){
Return 'abc ';
}
$ SmartyOb-> registerPlugin ("function", "template function name", "registered function name ");
Call on the template:
{Template function name = value ....}
How do I transmit parameters ???
1. the maximum number of parameters is two,
All parameter passing on the first received template,
The second parameter receives the current smarty object.
Cache
Data in the database is cached.
Benefits: reduces the pressure on mysql servers
1. specify the directory for storing cached files
$ SmartyOb-> setCacheDir (ROOT. "day02/cache ");
2. enable cache
$ SmartyOb-> caching = true;
3. specify the cache lifecycle.
$ SmartyOb-> cache_lifetime = 100; // second
Purpose: Determine whether the cached file exists and is valid
$ SmartyOb-> isCached ("template name and path", id)
$ SmartyOb-> display ("template name and path", id );
The above introduces the new arrival -------- smarty, including the content, hope to be helpful to friends who are interested in PHP tutorials.