Views and templates for ThinkPHP

In short, a controller corresponds to a view, and a method corresponds to a template. Let's go to the figure above.

 

2. Assign value to template

Assign() function is used to assign template. The first parameter of assign() function is to give a user-defined name to the value, and the second parameter is the value

Chestnut:

public function testview(){
   $date=Db::name("goods")->select();//Database query commodity table returns a dataset
   $this->assign('date',$date); //Give dataset to date
   return $this->fetch();
}

Then the corresponding template will get the date data set. Here is how to use the data set in the template and directly add the code:

<table>
   <tr>
        <th>commodity ID</th>
        <th>Trade name</th>
        <th>commodity price</th>
        <th>operation</th>
   </tr>
   {foreach $date as $value}
       <tr>   
        <th>{$value.id}</th>
        <th>{$value.goods_name}</th>
        <th>{$value.price}</th>
        <th><a href="#"> delete < / th >
       </tr>
    {/foreach}
</table>

III. how to comment in the template

Ordinary HTML annotation cannot annotate the content in {foreach}, because ThinkPHP will find "{}" to process the content in braces, so here we use a special method to annotate, such as {/ * annotated content * /} to annotate the following chestnut

<table>
   <tr>
        <th>commodity ID</th>
        <th>Trade name</th>
        <th>commodity price</th>
        <th>operation</th>
   </tr>
<!--For multiline annotation{/*Content of notes*/}-->
{/*   
{foreach $date as $value}
       <tr>
    <!--Use {/ / comment content} -- >   
        {//<th>{$value.id}</th>}
        <th>{$value.goods_name}</th>
        <th>{$value.price}</th>
        <th><a href="#">Delete </th>
       </tr>
    {/foreach}
*/}
</table>

IV. using functions in templates

strtoupper, md5 and other functions can also be used in the template. The following shows how to use them.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>test</title>
</head>
<body>
     <!--
  The use function in the template, for example, passes in a jack,Use strtoupper()Function to uppercase
     -->
    {$name | strtoupper}
     <br>
    <!--call md5 Function to name encryption-->
    {$name | md5}<br>
    <!--Multiple uppercase letters after function encryption-->
    {$name | md5 | strtoupper}<br>
    <!--Use specified class Methods in, such as call VERSION Obtain ThinkPHP Version number-->
    {:think\\APP::VERSION}
</body>
</html>

Tags: PHP Database

Posted on Mon, 02 Dec 2019 01:13:47 -0500 by jsantama