Smarty methods for customizing functions in the template engine, Smarty template engine functions _php tutorials

Source: Internet
Author: User
Tags smarty template

Smarty methods for customizing functions in the template engine, Smarty template engine functions


The example of this paper describes the Smarty custom function method, share to everyone for your reference. Specific as follows:

Purpose of this example: Output times con content (output 4 times Hello World)

File 1:
Copy the Code code as follows: <?php
Create a Smarty Object
Require_once ("./libs/smarty.class.php");
$smarty = new Smarty ();
Customizing a function
Description: (1), $arr as an array, (2), TPL invocation form {test times= "4" size= "5" con= "Hello,world" color= "Red"}
function test ($arr) {
$str = "";
for ($i =0; $i < $arr [' Times ']; $i + +) {
$str. = "". $arr [' con ']. "";
}
return $str;
}
Registration function Registerplugin
$smarty->registerplugin ("function", "Test", "test");//The second parameter is the name of the function called by the template file, variable, and the third parameter is the function name defined above; corresponding to a corresponding relationship

$smarty->display ("Temp.tpl");
?>

Template file: Temp.tpl
Copy the Code code as follows:

Smarty Use of custom functions


{Test times= "3" con= "Hello World" size= "3" color= "green"}

Note: Smarty 3.1.8 already does not support the registration function register_function and should be replaced with Registerplugin

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/946748.html www.bkjia.com true http://www.bkjia.com/PHPjc/946748.html techarticle Smarty The method of customizing functions in the template engine, Smarty template engine functions This article describes the Smarty custom function method, share to everyone for your reference. As follows: This example ...

  • 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.