View of thinkphp frame _php Tutorial

Source: Internet
Author: User
First, the View

1, the composition of the View components:

1) View class

View class

Smarty Class

2) Template

tpl/Project/Module/***.html

The view class is responsible for reading the template content and implementing the string substitution, which is eventually output to the user

2. Template definition

The default template file definition rules:

Template directory/[group name/] Module name/action name + template suffix

Tmpl_template_suffix

In general, the suffix of the template is broadly used in the following ways:

. html

. htpl

. TPL

3. Separators

Because each template designer's habits are different, some people are accustomed to using "{}" some people use the <{}> {*}

In the configuration file, you can set the following two configuration options, which represent the delimiter for the configuration template

' Tmpl_l_delim ' = ' <{',

' Tmpl_r_delim ' = '}> ',


4. Template assignment and output

1) $this->assign (' template variable name ', variable);

$var = ' mobile phone ';

$this->assign (' var ', $var);

$this->display (' Test ');


2) $this->assign (array variable);

<{$ array element Subscript}>


<{$price}>

<{$address}>

$var = ' mobile phone ';

$this->assign (' var ', $var);

$arr [' price '] = 33.3;
$arr [' address '] = ' Beijing ';
$this->assign ($arr);
$this->display (' Test ');
}

3) $this->display (' Operation name ')

The specified operation name under the current module. HTML Template

4) $this->display (' module: Operation name '); Templates are calls that can be called across modules.


Refers to the specified action name under the specified module. HTML Template

5) $this->display (' operation ', ' Output code ', ' output type ');


Cross-module output
$this->display (' User:login ', ' utf-8 ', ' text/html ');

5. Template substitution (template constants)

In the project often to reference CSS, JS, pictures of the resource needs to be referenced.


__PUBLIC__: Public directory for the current Web site

__app__: The URL address of the current project

__group__: The URL address of the current group

__URL__: URL address of the current module

__ACTION__: URL address of the current action

In the template in TP, you can use the above template constants, which represent different strings, generally when you need to reference the URL, you can use the above constants

By default: If we visit:

Localost/index.php/home/product/test, when the template uses the __public__ template constant, then its value points to the Apache Htdocs directory, but if we have multiple projects, then there will be conflicts, how to solve?

The way to solve it:

1) Modify the configuration file

In the configuration file, you can configure a call tmpl_parse_string option that defines the template values that are used in the template


Then, in the template, you can refer to the resource file under the current project:


2) Configuring the virtual host

Open the Host file:


Open httpd.conf


Remove the # before the configuration option above

Open the httpd-vhosts.conf file to add a new virtual host setting


Restart Apache

Localhost---àapache/htdocs/

tp.com-----àapache/htdocs/tp/

6. Get content

$this->fetch ();


Display: Read template, replace content, output

Fetch: read template, replace content, return string (primarily used to generate static pages)

Second, the template

1. Template Comment:

L {/* Comment content */}

L {//comment content}

Template annotations in TP are primarily for template designers or program designers

2. Variable output:

The program assigns values to the template

Common variables

$name

Array variables

$row

Object variables

$obj

code example:

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 configuration file


4. Using 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 {+ + $a}

L--{$a--} or {--$a}


7. Built-in label

<{$title}>

<标签>

L Closed Label


L Open Label

8. Include files

Is the entry file location based on the project.

./tpl/admin/public/header.html

We put the common parts of the Web page in the header.html and footer.html two public template pages, using the include reference in the home page


The path is too long when referencing the file, how to solve it?

L Format:

File (required): Resource file

Type (optional): Resource file type, default = JS

The starting path is the public (__public__) directory of the Web site

Use namespace mode

Directory. Directory. File name


10. Volist Label

Used to iterate over an array element

L Format:

{$vo. ID}

{$vo. Name}

L name (required): the array variable to traverse

L ID (required): Current array element

L Offset: offset to output data

L Length: The lengths of the output data, you need to specify offset

L Key: Loop index key value defaults to I


11. Foreach Label

Used to iterate over an array variable

Grammar:

{$vo. ID}

{$vo. Name}

Name: The array variable to traverse

Item: variable name to hold the current element

If you have special needs, use Volist, otherwise use foreach


12. For label

{$i}

Property:

L Start (required): Loop variable start value

L End (required): Loop variable end value (not included)

L name (optional): Loop variable name, default value I

L Step (optional): Step value, default value is 1


13. Switch label

L Format:

Output Content 1

Output Content 2

Default condition


14. Empty Label

L name is a null value

15. Assign Label

L

16. If label

L IF

L ElseIf

L Else


When judging, you need to use the following connector

L eq or Equal: equals

L NEQ or notequal: Not equal to

L GT: Greater Than

L EGT: greater than or equal to

L LT: less Than

L ELT: Less than or equal to

L Heq: Constant equals

L Nheq: not constant equals

17. Using 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

Recommendation: Use PHP code as little as possible in the template


http://www.bkjia.com/PHPjc/477228.html www.bkjia.com true http://www.bkjia.com/PHPjc/477228.html techarticle One, view 1, the composition of the View component: 1) The View Class View class Smarty Class 2) Template tpl/project/module/***.html View class is responsible for reading the template content, and implements the string substitution, finally ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.