PHP Smarty Template Custom Plugin

Source: Internet
Author: User
Tags smarty template

Plug - ins

is loaded on demand. Plugins are loaded when specific modifiers, functions, resources, and so on are called.

Plugin directory variables

Can be a string of a path or an array that contains multiple paths.

How to install plug-ins

is to put the plug-in files directly in these directories, Smarty will be automatically used as needed.

    1. Set the plugin directory (create a new Myplugins folder)

D:\wamp\www\test\smarty\libs\plugins\

D:\wamp\www\test\smarty\libs\myplugins\

Define the root directory define (' root ', str_replace ("\ \", "/", DirName (__file__)). " /");///Note Add a plug-in to the system by default, you will not be able to use the default system plug-in $smarty->setpluginsdir (Array (ROOT). /libs/plugins/",//system default set path ROOT." /libs/myplugins/",//Custom));

2. defining Plug-in functions

<?php/* * 1. Determine the plug-in location $smarty->setplugsdir (); * 2. The file name must have a rule, in order to determine the file name by the filename modifier. php * 3. The function name must have a rule in order to find this function in this file * 4. Parameters must have a rule the first one has to be processed. The second is the parameter to use */FU Nction Smarty_modifier_mystyle ($str, $size = "3", $color = "Pink") {return ' <font color= ' {$color} ' siz        E= ' {$size} ' >{$str}</font> "; }

3. Template Call plugin (decorator)

<{$sex |mystyle:6:red}>

4. Browser Output

Male x

Attach PHP page code

<?php//Create Smarty Object require_once './libs/smarty.class.php ';//define root directory define (' root ', str_replace ("\ \", "/", DirName (__ file__)). " /");//Instantiate Smarty Class $smarty=new smarty ();//Set delimiter $smarty->left_delimiter=" <{"; $smarty->right_delimiter="} > ";//set to false the bounding symbol can have spaces around $smarty->auto_literal = false;//Add a plugin to the directory//$smarty->setpluginsdir (ROOT.") /libs/myplugins/");//pay attention to adding a plug-in, to add the system default path to add the default system can not use the plug-in $smarty->setpluginsdir (" ROOT. " /libs/plugins/",//system default set path ROOT." /libs/myplugins/",//Custom));//Get the directory of the plugin var_dump ($smarty->getpluginsdir ());//variable Assignment $smarty->assign (' sex ', ' male X ');//variable Output $smarty->display (' Hello.tpl ');? >


This article is from the "Jin Sha Harbor" blog, please be sure to keep this source http://11410485.blog.51cto.com/11400485/1843672

PHP Smarty Template Custom Plugin

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.