Table of Contents
1. 使用registerPlugin()方法扩充变量调解器插件
2. 以特定文件方式扩充变量调解器插件
Home Backend Development PHP Tutorial Smarty3:自定义变量调解器插件

Smarty3:自定义变量调解器插件

Jun 20, 2016 pm 01:00 PM

Smarty3:自定义变量调解器插件

如果有一些变量在模版中需要特殊处理,系统中默认的变量调节器又没有提供这样的功能,就可以自定义变量调节器。smarty提供了两种扩充插件的机制:

通过Smarty对象中的registerPlugin()方法,将PHP编写的函数,注册到Smarty对象中,并在模版中使用

像系统默认的变量调节器一样,在Smarty库文件所在目录下的Plugins目录中,创建一个特定的文件扩展插件。

1. 使用registerPlugin()方法扩充变量调解器插件

registerPlugin()有三个参数:

使用字符串”modifier”指定插件的类型为修改器

插件的函数名称

定义的PHP回调函数

例如:在PHP脚本中声明一个函数为test(),将其注册为Smarty调解器”mystyle”,如下所示:

<?php //加载smarty初始化文件
include "init.inc.php";
//向模版中分配一个字符串变量var
$smarty -> assign("var","这是一个字符串的数据,看看样式变量");

//使用registerPlugin()方法,将函数test()动态注册为模版中可以使用的修改器mystyle函数
$smarty->registerPlugin("modifier","mystyle","test");
//声明一个函数,为Smarty扩充修改器
function test ($var, $color, $size){
    return '<font color="'.$color.'" size="'.$size.'">'.$var.'</font>';
}

$smarty->display("test.tpl");
/*模版中通过使用
{$var|mystyle:"red":7}{*自定义的变量调解器mysql,将变量$var的字体改为红色和7号字*}
*/
Copy after login

除了可以将自定义函数注册为变量调解器函数,也可以将PHP中的系统函数直接使用registerPlugin()方法注册为插件,不过要注意确保PHP系统该函数的第一个参数是要处理的变量。否则需要自定义一个函数调整参数位置。

<?php //加载smarty初始化文件
include "init.inc.php";
//向模版中分配一个字符串变量var
$smarty -> assign("var","这是一个字符串的数据,看看样式变量");

//直接将PHP系统函数substr(),注册成变量调解器函数substr
$smarty->registerPlugin('modifier','substr','substr');

//因为PHP系统函数preg_match()函数的第一个参数不是要处理的变量,所以自定义demo()函数重新注册
function demo($var, $reg,$text){
    return preg_match($reg,$text,$var);
}
$smarty->registerPlugin('modifier','regrep','demo');

$smarty->display('test.tpl');
Copy after login

2. 以特定文件方式扩充变量调解器插件

由于使用registerPlugin()方法,函数在PHP程序中声明,但是不在PHP程序中使用,和PHP脚本的其他函数混杂在一起,会导致PHP程序逻辑混乱。可读性差,因此建议以特定的文件方式扩充插件。

需要遵循smarty定义的规则。包括插件声明位置、文件的命名,函数的命名,参数的规则等的约束。

插件声明位置

由于自带的默认插件都在smarty类库下的plugins目录下。所以最好用addPluginsDir()方法指明自定义插件的位置。

    $smarty->addPluginsDir(ROOT."plugins");//自定义插件目录
Copy after login

文件命名方式

modifier.修改器名称.php      //以modifier为前缀,中间使要声明的修改器名称,以php结束
Copy after login

函数命名规则

smarty_modifier_修改器名称()     //函数名称要以smarty_modifier_为前缀,再加上修改器名称
Copy after login

参数说明

参数的第一个参数会自动传入要修改的变量。修改器中的其他参数从第二个参数开始。

修改上述的修改器:修改器文件命名为modifier.mystyle.php,放在由addPluginsDir()方法添加的插件目录下:

<?php //自定义修改器mystyle,以后就可以直接在任意模版中使用了.例如:{$var|mystyle:"red":7}
function smarty_modifier_mystyle($var,$color,$size){
     return '<font color="'.$color.'" size="'.$size.'">'.$var.'';
     }
Copy after login


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

Customizing/Extending Frameworks: How to add custom functionality. Customizing/Extending Frameworks: How to add custom functionality. Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

What exactly is the non-blocking feature of ReactPHP? How to handle its blocking I/O operations? What exactly is the non-blocking feature of ReactPHP? How to handle its blocking I/O operations? Apr 01, 2025 pm 03:09 PM

An official introduction to the non-blocking feature of ReactPHP in-depth interpretation of ReactPHP's non-blocking feature has aroused many developers' questions: "ReactPHPisnon-blockingbydefault...

See all articles