How PHP built-in functions are shared by instances invoked in Smarty

Source: Internet
Author: User
Clevercode found that calling PHP built-in functions in Smarty can be implemented by | | There are no spaces before and after. If it is an array, count needs to be added @.

1 parameters at a time:

{' param1 ' |functionname}}

For example

{{$tmpStr |strlen}}

2 parameters at a time:

{' param1 ' |functionname: ' Param2 '}} {$tmpStr |substr: ' 1 '}

Multiple parameter times:

{' param1 ' |functionname: ' param2 ': ' Param3 ' ...}} {{$tmpStr |substr: ' 1 ': ' 2 '}} {' A ' |str_replace: ' A ': $tmpStr}}

1 Marty to determine whether it is empty

The following code PHP is assigned to TMPSTR is empty or not assigned; then output in Smarty is 0. This is the built-in function called PHP strlen
PHP Code:

$smarty->assign (' tmpstr ', '); Smarty Code: {{if $tmpStr |strlen > 0}} 1{{else}}    

2 Marty to find the array length


The array length is evaluated using PHP's built-in functions Is_array and count, such as code output 5
PHP Code:

$smarty->assign (' Tmparr ', Array (1,2,3,4,5));

Smarty Code:

{{if $tmpArr |is_array && $tmpArr | @count > 0}}    {{$TMPARR | @count}} {{Else}}    0{{/IF}}

3 Marty String substitution

The following code is to replace B in tmpstr with C

$smarty->assign (' tmpstr ', ' ABB '); {' B ' |str_replace: ' C ': $tmpStr}}

Output:

Acc

4 Marty Intercept Field

$smarty->assign (' tmpstr ', ' ABB '); {{$tmpStr |substr: ' 1 '}}

Output:

Bb
{{$tmpStr |substr: ' 1 ': ' 1 '}}

Output:

B

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.