Ways to invoke PHP's built-in functions in Smarty _php tips

Source: Internet
Author: User
Tags function prototype smarty template

I believe a lot of friends do not know, you can call in the Smarty template PHP built-in functions, we look at its use.

Template writing:
{' param1 ' |functionname: ' param2 ': ' Param3 '}

PHP function prototype:
echo functionname (' param1 ', ' param2 ', ' param3 ');
Instance:
{' 1234567 ' |substr: ' 1 ': ' 2 '}

the following is related to the order of the parameters of the function

{' A ' |str_replace: ' A ': ' ABCD '}

Directly to the, directly in PHP to write a function call, do not need to register the modifier.

Further research: finding arrays can be an error.

An array of arrays is assigned to smarty, assuming that the Smarty's delimiters are ' {' and '} '.

As you can see in a lot of data, the length of the array is required in the smarty, and the |count method call is used after the array. The length of the array is obtained through {array|count}. But today, when writing a template, you find that you don't get the length of the array, just the string array that's returned. That is, it simply returns the result of the {array} and does not return the length of its array.
Looking at the Smarty\plugins folder, I found that there was no method for count, that is, Count is a direct call to the method in PHP.

Later, through the data on the network, it was found that you can add @ before count to get the correct length of the array. Further look at the source code of Smarty, found that the smarty of the property adjuster after the method name processing, will be the front plus @ for special treatment. So make a judgment: when you call a defined function in PHP in the property adjuster in Smarty, you can express it by adding @.
1. When testing a method that is an array of types, it is found that no @ symbol can be faulted. For example, to call the Count method on an array to find the length of the array, you can call {array| @count}, and you can pass the {array| @end} if you want the last set of data to be derived from the arrays by calling the end method.
2, in the relevant function of the string test, found that the addition of @ can be normal calls.
3, the other has not been carefully tested.

It is not encouraged to invoke complex PHP functions in Smarty, because Smarty's intention is to implement code and template separation, do not deviate from the original intention of others.

Template writing:
{' param1 ' |functionname: ' param2 ': ' Param3 '}

PHP function prototype:
echo functionname (' param1 ', ' param2 ', ' param3 ');

Instance:
{' 1234567 ' |substr: ' 1 ': ' 2 '}
Here's a comparison between the odd and the parameter order of the function.
{' A ' |str_replace: ' A ': ' ABCD '}

Smarty Call a custom function
Calling a custom function requires register_function () to register
Here's an example of a common string-cutting

Functions are as follows

Copy Code code as follows:

<?php
function Smartylen ($params) {
Extract ($params);
$len =strlen ($text);
$max = $length;
for ($i =0; $i < $length; $i + +) {
$CHR =substr ($text, $i, 1);
if (Ord ($CHR) >0x80)//character is Chinese
{
$length + +;
$i + +;
$len--;
}
}
$str =substr ($text, 0, $length);
if ($len > $max) $str. = "...";
return $str;
}

Registration function
$smarty->register_function (' Len ', "Smartylen");

Template calls
{len text= "test" length= "1"}//Note that the text and length here are actually the parameters of the 2 parameter functions in the function must be the same as the internal variable name of the function. In fact, the return is the number of groups, in the function with extract ($params), the array of variables into the current symbol table.

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.