Thinkphp 3.1 QuickStart: Variable output

Source: Internet
Author: User
Keywords QuickStart thinkphp variable output
Tags aliyun data email example how to how to use http internal

In the previous chapter we learned how to assign a variable to a template variable through the Assign method, which we'll look at in detail how to use the label output template variable in a template.
Note that this article is only intended for use with the internal template engine, and if you use a smarty or other template engine, refer to its associated variable output syntax.

Variable output

The method of variable output is very simple, for example, in the controller we assign values to template variables:

$http://www.aliyun.com/zixun/aggregation/11696.html ">name = ' thinkphp '; $this->assign (' name ', $name); $this-> Display ();

You can then use it in a template:

hello,{$name}!

The results of the template compilation are:

hello,<?php Echo ($name); >!

In this way, the runtime is displayed in the template:

hello,thinkphp!

Note The template label cannot have any spaces between {and $, otherwise the label is invalid. So the following label

hello,{$name}!

The name variable will not be output normally, but the output is kept unchanged directly:

hello,{$name}!

Normal label default start tag is {, end tag is}. You can also make changes by setting Tmpl_l_delim and Tmpl_r_delim. For example, we define in the project configuration file:

' Tmpl_l_delim ' => ' <{', ' Tmpl_r_delim ' => '}> ',

The above variable output label should be changed to:

hello,<{$name}>!

Later, we all use the default label definition to explain.
The variable output of the template label differs according to the type of the variable, which we have just exported is a string variable, and if it is an array variable,

$data [' name '] = ' thinkphp '; $data [' email '] = ' thinkphp@qq.com '; $this->assign (' data ', $data);

So, in the template we can output in the following way:

name:{$data. name}email:{$data. Email}

Or it works in the following ways:

name:{$data [' name ']}email:{$data [' Email ']}

When we want to output a multidimensional array, we tend to use the latter approach.
If the data variable is an object (and contains the name and email two attributes), you can output it in the following way:

name:{$data: name}email:{$data: Email}

Or

name:{$data->name}email:{$data->email}

System variables

Normal template variables need to be assigned first before they can be exported in the template, but system variables are not needed and can be exported directly to the template, and the output of the system variable usually starts with the {$Think, for example:

{$Think. Server.script_name}//Output $_server[' script_name '] variable {$Think. session.user_id}/Output $_session[' user_id '] variable {$ Think.get.pageNumber}//Output $_get[' pagenumber ' variable {$Think. cookie.name}//Output $_cookie[' name ' variable

Supports output $_server, $_env, $_post, $_get, $_request, $_session, and $_cookie variables.
You can also output constants

{$Think. Const.module_name}

or use it directly.

{$Think. Module_name}

Output configuration parameters are used:

{$Think. Config.db_charset} {$Think. Config.url_model}

Output language variables can be used:

{$Think. Lang.page_error} {$Think. Lang.var_error}

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.