thinkphp5-----Use of functions in templates

1. Use php functions in templates

   In thinkphp's html, we often encounter some variables that are difficult to process directly from the php control side. These variables are only suitable for processing when looping output in the template. At this time, we need to use functions in the template.

 

  1.1 Use php functions for output templates

 

{$data.name|md5} //Encrypt the name variable in the template with md5

 

  Translate this sentence into php language:

<?php echo (md5($data['name'])); ?>

 

 

  1.2 Multiple parameters in the function need to be called

    1.2.1 Use the previously output variable as the second parameter of the following function

{$create_time|date="y-m-d",###}

     Explanation: The date function passes in two parameters, each parameter is separated by a comma, where the first parameter is y-m-d, the second parameter is the create_timevariable to be output before, because this variable is the second parameter, so you need to use ### Identifies the variable location.

 

  Translate to php language:

<?php echo (date("y-m-d",$create_time)); ?>

      

      1.2.2 Use the previously output variable as the first parameter of the following function

 

{$data.name|substr=0,3}

 

or

 

{$data.name|substr=###,0,3}

 

 

 

 

   translate into php language

<?php echo (substr($data['name'],0,3)); ?>

 

    1.3 Process multiple functions on a variable

 

{$name|md5|strtoupper|substr=0,3}

 

    Each function is separated by 丨 symbol, and the function execution order is called from left to right

or:

 

{:substr(strtoupper(md5($name)),0,3)}

 

 

   Compile into php language:

 

<?php echo (substr(strtoupper(md5($name)),0,3)); ?>

 

 

 

2. Variables are output in templates using custom functions

  In the project, in addition to some php functions, we can also write our own custom functions in the project application directory /common/function.php according to the actual needs of our own projects

  

  Important note: There cannot be a space between the { and $ symbols, and there is no problem with the space behind the parameter;

       ### indicates the parameter position of the template variable itself;

       multiple functions are supported, and spaces between functions are supported;

       function masking function is supported, in the configuration A list of prohibited functions can be configured in the file;

       variable caching is supported, and repeated variable strings are not parsed multiple times.

  2.1 The use of custom functions

    Simple custom functions are basically the same as using php functions.

  

  2.2 Advanced use of custom functions

    

{$varname|function1|function2=arg1,arg2,### }

 

 

    Translated into php code:

 

<?php echo (function2(function1(arg1,arg2,$varname)); ?>

 

 

    2.3 Cases

I write a method in function.php

function Cate($cid){
$ Cate = D ('Cate');
$Cate=$Cate->where('id='.$cid)->find();
return $Cate['title'];
}

Call this custom function in the template:

{$ vo.cid | cate = ###}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324473437&siteId=291194637