ThinkPHP framework view. I. Composition of View and View components: 1) View and View Smarty 2) template Tpl project module ***. the html view class reads the template content and replaces the string.
1. View component composition:
1) View class
View class
Smarty class
2) template
Tpl/project/module/***. html
The View class reads the template content, replaces the string, and finally outputs the content to the user.
2. Template definition
Default Template File definition rules:
Template directory/[group name/] module name/operation name + template suffix
TMPL_TEMPLATE_SUFFIX
Generally, the template suffix uses the following types:
. Html
. Htpl
. Tpl
3. delimiter
Because each template designer has different habits, some people are used to "{}" and some use <{}> {**}
In the configuration file, you can set the following two configuration options to indicate the delimiter of the configuration template.
'Tmpl _ L_DELIM '=>' <{',
'Tmpl _ R_DELIM '=>'}> ',
4. Template assignment and output
1) $ this-> assign ('Template variable name', variable );
$ Var = 'cell phone ';
$ This-> assign ('var', $ var );
$ This-> display ('test ');
2) $ this-> assign (array variable );
<{$ Array element subscript}>
<{$ Price}>
<{$ Address}>
$ Var = 'cell phone ';
$ This-> assign ('var', $ var );
$ Arr ['price'] = 33.3;
$ Arr ['address'] = 'Beijing ';
$ This-> assign ($ arr );
$ This-> display ('test ');
}
3) $ this-> display ('Operation name ')
Define the operation name .html template in the previous example
4) $ this-> display ('Module: Operation name'); // The template can be called across modules.
Reference refers to the specified operation name. HTML template
5) $ this-> display ('operation', 'output encoding', 'output type ');
// Cross-module output
$ This-> display ('User: login', 'utf-8', 'text/html ');
5. Template replacement (Template constant)
Css, js, and image resources are often referenced in projects.
/Public: Public directory of the current website
/Index. php: URL address of the current project
/Index. php: the URL address of the current group
/Index. php/Article: URL address of the current module
/Index. php/Article/detail: URL of the current operation
In the template of tp, you can use the preceding template constants, which represent different strings. Generally, you can use the preceding constants when you need to reference a url.
By default, if we access:
Localost/index. php/home/product/test. if the/Public template constant is used in the template, its value points to the htdocs directory of apache, but if we have multiple projects, there will be conflicts. how can this problem be solved?
Solution:
1) modify the configuration file
In the configuration file, you can configure an option called TMPL_PARSE_STRING, which can define the values that are common in the template.
Then, in the template, you can reference the resource files under the current project as follows:
2) configure the VM
Open the host file:
Open httpd. conf
Remove # from the preceding configuration options
Open the httpd-vhosts.conf file and add new VM settings
Restart apache
Localhost --- à apache/htdocs/
Tp.com ----- à apache/htdocs/tp/
6. get content
$ This-> fetch ();
Display: Read template, replace content, and output
Fetch: Read template, replace content, and return string (mainly used to generate static pages)
II. Template
1. template comment:
L {/* Comment */}
L {// comment content}
Template Annotations in Tp are mainly for template designers or program designers.
2. variable output:
The program assigns a value to the template.
Common variables
$ Name
Array variables
$ Row
Object variable
$ Obj
Sample code:
Php program:
Template program:
3. system variables (system variables in the template)
L $ Think. server $ _ SERVER
L $ Think. get $ _ GET
L $ Think. post $ _ POST
L $ Think. request $ _ REQUEST
L $ Think. cookie $ _ COOKIE
L $ Think. session $ _ SESSION
L $ Think. config read the configuration file
4. use functions
L format
{$ Name | fn1 | fn2 = arg1, arg2 ,###}
5. default value
{$ Variable | default = "default value "}
6. Operators
L + {$ a + $ B}
L-{$ AB}
L * {$ a * $ B}
L/{$ a/$ B}
L % {$ a % $ B}
L ++ {$ a ++} or {++ $}
L -- {$ a --} or {-- $}
7. built-in labels
<{$ Title}>
<标签>
L closed labels
L open tags
8. include files
Is the project-based entry file location.
./Tpl/Admin/Public/header.html
We put the public webpage on header.htmland footer.html, and use include on the home page for reference.
When the above file is referenced, the path is too long. how can this problem be solved?
L format:
File (required): Resource file
Type (optional): type of the resource file. the default value is js.
The initial path is the Public (/Public) Directory of the website.
Namespace
Directory. Directory. file name
10. volist tag
Used to traverse array elements
L format:
{$ Vo. id}
{$ Vo. name}
L name (required): array variable to be traversed
L id (required): Current array element
L offset: the offset of the data to be output
L length: the length of the output data. you must specify the offset.
L key: the default value of the cyclic index key is I.
11. foreach label
Used to traverse array variables
Syntax:
{$ Vo. id}
{$ Vo. name}
Name: array variable to be traversed
Item: The variable name used to save the current element.
If you have special requirements, use volist. Otherwise, use foreach.
12. for tag
{$ I}
Attribute:
L start (required): start value of the cyclic variable
L end (required): end value of the loop variable (not included)
L name (optional): name of the cyclic variable. the default value is I.
L step (optional): step value. The default value is 1.
13. switch label
L format:
Output Content 1
Output Content 2
Default
14. empty tag
L Name is null
15. assign label
L
16. if tag
L if
L elseif
L else
Use the following connector when determining
L eq or equal: equal
L neq or notequal: not equal
L gt: greater
L egt: greater than or equal
L lt: less
L elt: less than or equal
L heq: constant equals
L nheq: non-constant equals
17. use php code
1) Echo "hello ";
2)
In the configuration file, there is an option to control whether the second method is available
TMPL_DENY_PHP can disable the second method.
Suggestion: use php code as few as possible in the template
Preview 1. View component composition: 1) View-class Smarty Class 2) template Tpl/project/module /***. the html view class reads the template content and replaces the strings...