thinkphp Frame View Details View views-templates (9)

Source: Internet
Author: User
thinkphp Frame View Details View-Template (ix)

Views are also a core part of thinkphp use:


First, the use of templates

A, Rules
under Templates folder [tpl]/[Group folder/][template theme Folder/] folder with the same name as module name [index]/and method name file [Index].html (. TPL)

--Replace the suffix name of the template file (modify the configuration file)

' Tmpl_template_suffix ' = '. TPL ',//change template file suffix name, default is HTML


B, modify the template file directory hierarchy
tpl/index/index.html ==> tpl/index_index.html (changed to this file directory form)
configuration file:
' Tmpl_file_depr ' = ' _ ',//Modify template file directory hierarchy

c, template theme (that is, the different templates of the site like QQ dress skin)
-->TP default is no theme, now simulates creating 2 theme templates for my and your folders
home/tpl/my/index/index.html
home/tpl/your/index/index.html
which template theme would you like to use to change the corresponding value in the configuration file
' default_theme ' + ' your ',//Set Default template theme, Access is the index under the your template
you need to create a new your folder under the TPL as a template theme folder

* How do I dynamically modify a template theme?
1. Prepare a function in the background, modify the default template item in the config.php file
2. Pass t= theme parameters via URL to modify different templates
' default_theme ' = ' your ',//Set default template theme
' Tmpl_detect_theme ' =>true,//auto-detect template Theme
' theme_list ' + ' your,my ',//list of supported templates themes to set up my and your templates first.
set it up so that access, followed directly with a T parameter t/your template file:
http://localhost/thinkphp/index.php/Index/index.html/t/my
Http://localhost/thinkphp/index.php/Index/index.html/t/your


second, the output template content
A, display
no parameters in 1.display
$this->display ();
2. Can take parameters
$this->display (other template files in this module folder);
$this->display (' index2 ');


$this->display (template file under other folders);
$this->display (' User:index ');//This module calls the User module's index file
//Note that only the public folder and the error.html within the TPL are required
//Do not need to have public module--publicaction.class.php
$this->display (' Public:error ');


$this->display (template file under the folder under Other topics);
//need to turn on theme support, default_theme ' = ' my '
$this->display (' My:Index:index ');


$this->display (a URL path);
$this->display ('./public/error.html ');//public location at the root of the site/public/error.html
//Almost no use
$this->display ('./public/error.html ', ' utf-8 ', ' text/xml ');//Template encoding display method (HTML or XML)


$content = ' This is the use of Show direct HTML code ';//Maybe it's the database. Directly to the output to
$this->show ($content); = = $this->show (' This is the use of Show direct HTML code ');

3.fetch Method
gets the contents of the template file, returned as a string
$content = $this->fetch (' Public:error ');
4.show Method
No template file required, can directly output template content
$content = $this->fetch (' Public:error ');
dump ($content);
$content =str_replace (' H1 ', ' I ', $content);
$this->show ($content);

third, the assignment in the template:
//$this->assign (' name ', ' Le June Yang ');
$this->name= ' Le June Yang 2 ';
$this->display ();
template variable output:
one, variable output
1. Scalar output--integer type floating-point character type 、、、
2. Array Output
$arr = Array (' k1 ' = ' leyangjun ', ' k2 ' = ' leyangjun2 ');
$this->assign (' name ', $arr);
$this->display ();
template output by:
{$name [1]}--indexed array
{$name [' K2 ']}--associative array or
{$name. K1}
3. Object Output
{$name: k}
{$name->k}
second, System variables (manual detailed introduction)
//http://localhost/thinkphp/index.php/index/add/name/leyangjun
{$Think. get.name}--url The name value, you can get the output directly in the template
{$Think. get.id}
third, use the function (template in the variables directly using the PHP function)
{$name |strtoupper} generated post-compilation file is visible in generating temporary file
{$name |date= ' Y m D h:i:s ', # # #}
Four, default value
{$name |default= ' here is the default value '}
v. Operators
+ - * / % ++ --
$this->assign (' name ', ten);
{$name + +}--11
  • Related Article

    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.