Method for customizing functions in the smarty template engine _ php instance

Source: Internet
Author: User
Tags smarty template
This article mainly introduces the methods for customizing functions in the smarty template engine. The example analyzes the definition, registration, and calling skills of custom functions, for more information about the smarty UDF method, see the following example. The details are as follows:

Purpose: To output times con content (output 4 times hello world)

File 1:

The Code is as follows:

<? Php
// Create a smarty object
Require_once ("./libs/Smarty. class. php ");
$ Smarty = new Smarty ();
// Customize a function
// Description: (1), $ arr is 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. = "". $ arr ['Con ']. "";
}
Return $ str;
}
// Register the registerPlugin Function
$ Smarty-> registerPlugin ("function", "test", "test"); // The second parameter is the name of the function called by the template file, variable; the third parameter is the name of the previously defined function.

$ Smarty-> display ("temp. tpl ");
?>

Template File: temp. tpl

The Code is as follows:


Use of smarty custom functions
{Test times = "3" con = "hello world" size = "3" color = "green "}


Note: The registry function register_function is not supported in smarty 3.1.8. Replace it with registerPlugin.

I hope this article will help you with php programming.

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.