I. View
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}>
<! -- Directly use the subscript of the array to retrieve data -->
<{$ Price }>< br/>
<{$ Address }>< br/>
$ 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
_ APP __: URL address of the current project
_ GROUP __: the URL address of the current GROUP
_ URL __: URL address of the current Module
_ ACTION __: 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, the value of the template points to the directory of apache htdocs, 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}>
<Tag>
L closed labels
<Br/>
L open tags
<Div> </div>
8. Include files
<Include file = "template file name"/>
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?
<Include file = "module name: Operation name" title = "Think"/>
Reference the specified template in the specified Template under the current project
Use include to include the footer and header files under the "Public module ".
The Include tag allows parameters to be passed to the template.
Template code:
9. Import files
In tp, several tag implementations (simplified) are provided to reference resource files.
<Js href = "_ PUBLIC _/Js/Common. js"/>
<Css href = "_ PUBLIC _/Css/common.css"/>
<Load href = "_ PUBLIC _/Js/Common. js"/>
L format:
<Importtype = 'type' file = 'file'/>
File (required): resource file
Type (optional): type of the resource file. The default value is js.
The starting path is the Public (_ PUBLIC _) Directory of the website.
Namespace
Directory. Directory. File Name
10. volist tag
Used to traverse array elements
L format:
<Volist name = "list" id = "vo">
{$ Vo. id}
{$ Vo. name}
</Volist>
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:
<Foreachname = "list" item = "vo">
{$ Vo. id}
{$ Vo. name}
</Foreach>
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
<Forstart = "1" end = "100">
{$ I}
</For>
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:
<Switch name = "variable">
<Case value = "value 1" break = "0 or 1">
Output content 1
</Case>
<Case value = "value 2"> output content 2 </case>
<Default/> default
</Switch>
14. empty tag
L <empty name = "name"> name is null </empty>
15. assign label
L <assign name = "var" value = "123"/>
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) <php> echo "hello"; </php>
2) <? Php echo "think";?>
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