3.ThinkPHP Getting Started---view

Source: Internet
Author: User

View: three components of MVC, responsible for the presentation and output of information

1. Creation of views

The location you create needs to be in a directory with the same name as the view directory remaining controller under the grouped directory.

2. Display of views

Both in smarty and tinkphp are used diaplay

$this->dispaly ();

$this->display (' template filename ');

$this->display (' directory name / template filename ' in the ' View Directory ');

3. variable Assignment 3.1 variable allocation (beginner)

in the actual development time is not only to show the template is so simple, often need to show the data, this time the variable is still in the controller's method, need to pass the data to the template and show, this process is called variable allocation, in thinkphp The system encapsulates a variable allocation method, assign, with the following syntax:

$this->assign (the variable name in the template, the variable name in the $php );

For example: in assigning variables in the test method in TestController

Public function Test () {
$var =date(' y-m-d h:i:s ',Time ());
$this->assign (' var ', $var);
$this->display ();
}

test.html How do I get the value of a variable? The method is as follows: {The variable name in template},

now Beijing time:{$var}

3.2 variable output (Advanced)

Simple variable output:

One-dimensional array output:

Two-dimensional array output:

Object Output :

Objects are typically saved to a variable after instantiation, and the variable can be assigned to the template.

3. variable delimiter

The default left and right delimiters in tinkphp are {}, which can be changed

5. template constant replacement mechanism

In the actual development of the introduction of some files often need to write complex path, then how to simplify the complex path, this time we can consider the template constant replacement mechanism

several common constants of the system default in thinkphp:

__module__// identifies routes starting from the domain name to the end of the packet

__controller__// identifies the route from the domain name onwards to the end of the controller

__action__// identifies the route from the domain name onwards to the end of the method

__public__// routes that identify the public directory under the site root directory

__self__// identifies routes starting from the domain name to the end (if no parameters are the same as 3 content)

in thinkphp the template constants are implemented through the template content substitution mechanism, not the definition of constants, and the substitution mechanism can view the behavior file /thinkphp/libraty/behavior/ contentreplacebehavior.class.php, the core of its template implementation is the substitution of strings

6. Template Content Acquisition method

1. in tinkphp and display method similar method called fetch

$this->display (); Presentation Templates $this-fetch (); Get the template -- has a return value

2. View notes:

normal annotations are output in the HTML source code:<!-- This is a comment --

module comments in thinkphp:

Line Comment: {// This is a line comment}

Quick Comment: {/* This is a block comment */}

7. System Variables

$Think. server// getting information about the server

$Think. get// equivalent $_get, get get request Information

$Think. post// equivalent $_post, get post request Information

$Think. request// equivalent $_request, get Request Information

$Think. cookie// equivalent $_cookie, get cookie Request Information

$Thin. config// equivalent $_config, gets The synthesis of all the configuration files in the thinkphp, gets the specified configuration if the element is specified ,

syntax Format: {$Think. Get.id}

8. using functions in views

Some templates are not used directly in real development, such as displaying timestamps in a view that need to be traversed to handle the format, which is relatively easy to use in this way.

8.1 Syntax:{$ variable | Name of function 1| Name of function 2= Parameters 1 , the parameter 2}

8.2 parameter Description:| is a variable modifier

$ Template Variable

function name 1: Identify the first functions to be used, parameter 1, parameter 2 to identify the parameters of function 2

8.3 case 1:

Note: When you need to use a function with only one parameter and the argument is the variable itself, # # can be omitted

when you need to use a function that has multiple parameters, but its first argument is the variable itself, you can omit the

Description of Function name: function name corresponding function must be PHP built-in function or is defined in the function library

Case 2: Define a string, intercept the first five of them and turn to uppercase {$str |substr=###,0,5|strtoupper}

9. Default Value

Syntax: {$ variable name |default= default }

10. operator

+  -  *  /  %  ++  --

11. File Introduction

The public parts of the site, such as the head, the bottom of a separate file, in the late use of the direct introduction of this section, maintenance only need to maintain a code to

thinkphp Template Tags:<include file='/admin/style.css ' >

12. Looping through

in thinkphp, the system provides 2 tags to implement the traversal of the array in the template.

Volist foreach

13.if label

<if conditon= ' conditional expression ' >

Output Results 1

<else if condition= ' conditional expression 2 '/>

Output Results 2

<else/>

Final output

Case:

Public function test12 () {
$day =Date(' N ',Time ());
$this->assign (' Day ', $day);
$this->display ();
}

<Body>
<ifcondition=' $day ==1 '>
Week1
<ElseIfcondition=' $day ==2 '/>
Week2
<ElseIfcondition=' $day ==3 '/>
Week3
<ElseIfcondition=' $day ==4 '/>
Week4
<ElseIfcondition=' $day ==5 '/>
Week5
<ElseIfcondition=' $day ==6 '/>
Week6
<Else/>
Week7
</if>
</Body>

14. PHP Tags

using PHP syntax format in the template, php tag tinkphp in the template supports two forms

14.1.PHP built -in php Tags:<? php code snippet ?>

14.2.Thinkphp :<php> code snippet </php>

generally do not write PHP tags in the view , but also do not recommend disabling php tags

3.ThinkPHP Getting Started---view

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.