Smarty a method for customizing functions in the template engine _php instance

Source: Internet
Author: User

The example of this article describes the Smarty custom function method, shared for everyone to reference. Specifically as follows:

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

Document 1:

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

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

Template file: Temp.tpl

Copy Code code as follows:
Use of {Test times= "3" con= "Hello World" size= "3" color= "green"}

Note: Smarty 3.1.8 has not supported the registration function register_function, should be replaced by Registerplugin

I hope this article will help you with your PHP program design.

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.