Iv. custom functions for Smarty templates

Source: Internet
Author: User

Custom functions for Smarty templates (common in this article)

Divided into three categories:

1. Variable Adjuster

2. Functions

3. Block function

Usage of three kinds of plugins:

1, the use of variable mediators,

< {$var|myfun:arg1:arg2} >

2. Function usage (much like using HTML tags)

Such as

<colorsizenum= "7"}>

The functions defined in PHP are:

$smarty->registerplugin ("function", "Myfun","one"); function One ($args,$smarty) {}   //$args color= "Red" size= "7" num= "7" These parameters are combined with an associative array, and the second parameter is automatically received by the $smarty object, if not written

3, the use of block functions,

Such as

<colorsize= "7"}> content <{/myfun  }>

The functions defined in PHP are:

$smarty->registerplugin ("block", "Myfun","I")Function ($args ,$content,$smarty) {}   //$args An associative array that combines these parameters for color= "Red" size= "7", The second parameter is the "content" to be displayed, and the third parameter is the $smarty object

There are two ways of doing this:

1. Use the Registerplugin () method in the Smarty object to register functions in PHP as functions in Smarty.

2, is the development of the Smarty plug-in (to separate specific files to add plug-ins)

1) Location of the Declaration

Can be created in the original plugins directory in the Smarty class library, or you can use the directory $smarty->addpluginsdir (directory) you specified//smarty3

2) Name of the file

modifier, modifier. Modifier name. php

Functions, functions. Name of the function. php

Block function, block function name. php

3) Naming of functions

modifier, Smarty_modifier_ modifier name ()

function, Smarty_function_ function name ()

Block function, Smarty_block_ block function name ()

4) Rules for parameters

modifier, smarty_modifier_ modifier name ($var, $arg 1, $arg 2, $arg 3)

function, Smarty_function_ function name ($args, $smarty)

Block function, Smarty_block_ block function ($args, $content, $smarty, $repeat)

$repeat parameter prevents the block function from being called repeatedly, and the first call is true and later false

<{myfun color= "Red" size= "7"}> content <{/myfun}> First Myfun true, second <{/myfun}> false

Iv. custom functions for Smarty templates

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.