Smarty
use of Day01-----smarty
Role: Separating the php+html, the program and the artwork
Use:
1. Download the source package
2. Directory structure
Libs
source files
Smarty.class.php
Template/templates file HTML file
Template_c//program run process, automatically generated, template compiled files
3. Instantiation
Require "Smarty.class.php";
$smartyOb =new Smarty ();
4. Configuration
Tell Smarty which directory to put the template HTML
$smartyOb->settemplatedir (path);
Tell Smarty which directory is the compiled file
$smartyOb->setcompiledir (PATH)
5. Display Template
$smartyOb->display ("Template name and path");
How to pass a value to a template
$smartyOb->assign (' template name ', passed variable);
How to invoke on a template
Template label
{$ name}
Array
{$ array name. Subscript}
Two-dimensional arrays
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 '),
)
Iterating through an array
{foreach array name as variable name}
{$ variable name. Title}
{/foreach}
Subscript value
<{$ variable name @key}> <{$ variable name @index}>
Number of Cycles
<{$ Variable name @total}>
The current cycle is the first few
<{$ Variable name @iteration}>
is the first cycle
<{$ Variable name @first}>
Is the last loop
<{$ Variable name @last}>
Case:
Implements an interlaced background color change
<{if condition}>
Code
<{/if}>
Practice:
Underline (dashed) each Li, except for the last line
Learn what:
<{section name=s1 loop= The array to be traversed}>
<{$ the array to be traversed [S1]. Subscript}>
<{/section}>
Property:
<{$smarty .section.s1.total}>
<{$smarty .section.s1.index}>
<{$smarty .section.s1.iteration}>
<{$smarty .section.s1.last}>
<{$smarty .section.s1.first}>
Practice:
Use section to achieve different interlaced background colors
Variable adjuster
The template label delimiter is specified:
<{}>
$smartyOb->left_delimiter= "<{";
$smartyOb->right_delimiter= "}>";
Day02---------, review the contents of DAY01, expand new knowledge
Smarty
1, Smarty, placed in the PHP main program
2, Configuration
$smartyOb->settemplatedir ()
$smartyOb->setcompiledir ()
$smartyOb->left_delimiter ()
$smartyOb->right_delimiter ()
3, pass the value
$smartyOb->assign (template variable name) to the template, variable);
4, display template
$smartyOb->display (path);
{$ name}
{$ array. subscript}
{foreach array name as variable}
{$ variable name}
{/foreach}
Tag property:
{$ variable name @key}
{$ variable name @index}
{$ Variable name @iteration}
{$ variable name @total}
{$ variable name @first}
{$ variable name @last}
{if condition}
{Else}
{/if}
Variable adjuster:
{ First argument | function name: second argument: ...}
Article details page:
02_detail.php?id=5
1, configure Smarty
2, receive article ID
3, Instantiate PDO
4, block SQL statements
5, execute
6, get data
7, Pass a value
8, display template
9, set template label to the template, use the data passed by the main program
Page splitting
{include file= "path to Template"}
Registration function
function Fun1 () {
Return ' ABC ';
}
$smartyOb->registerplugin ("function", "template function name", "Registered functions name");
Called on the template:
{Template function name name = Value Name = value ...}
How to send a reference???
1. A maximum of two parameters,
All the arguments on the first received template,
The second parameter receives the current Smarty object.
Cache
The data in the database is cached.
Benefit: Reduce the pressure on MySQL server
1. Specify the directory where the cache files are stored
$smartyOb->setcachedir (ROOT. " Day02/cache ");
2. Turn on cache
$smartyOb->caching=true;
3. Specify the life cycle of the cache.
$smartyOb->cache_lifetime=100;//seconds
Function: Determine if the cache file exists and is valid
$smartyOb->iscached ("Template name and path", id)
$smartyOb->display ("Template name and path", id);
Newcomer--------Smarty