Home > Article > Backend Development > php smarty Chinese interception plug-in development example_PHP tutorial
Smarty is undoubtedly the most popular and famous template engine in PHP development. By using this template engine, it brings great convenience to our development work. Let’s share the smarty plug-in technology (taking creating a php smarty Chinese string interception as an example), making full use of the various features of smarty, making php smarty a sharper tool in our hands, making our work faster and more efficient.
(1) First we need to know some knowledge about smarty and its plug-ins
1. What is smarty?
Smarty is a template PHP template engine written in PHP. It is a template system recommended by php.net.
2. What is smarty plug-in?
Smarty plug-ins refer to the plugins in smarty, which are some functional control statements embedded in the template. The Variable Modifiers (variable adjustment) in smarty are actually some built-in plug-ins.
3. How does the plug-in work?
In the smarty template, the plug-in is loaded dynamically when using the calling statement. You can put the plug-ins you have written into the plugins directory under the lib directory in the smarty directory, so that when these plug-ins are used in the template, they will be loaded dynamically. will be loaded automatically.
4. How many types of plug-ins are there?
The types of smarty plug-ins are: function, modifier, block, compiler, prefilter, postfilter, outputfilter, resource, insert. In this article we only share how to develop function type plug-ins. The development methods of other types are similar. You can Try to imitate.
5. How to name the plug-in?
File name format:
type.name.php
Type refers to the type, and the ones mentioned above are its selection range;
name: Customized plug-in name, showNews is used in this article to name it;
Function name:
smarty_type_name() smarty: fixed name at a fixed position; type is consistent with the type of the file name, name is consistent with the name in the file name
(2)Now that the basic knowledge is understood, let’s start development. Copy the following code into a file, name it modifier.truncate_cn.php file, and then copy the file to the smarty/lib/plugins/ directory (note that the directory format is not fixed, individuals can choose it according to their own circumstances, but it must be placed in the plugins directory).
/* *作者:http://www.phpernote.com/ *时间:2013年1月31日06:31:52 *作用:截取中文字符串 */ function smarty_modifier_truncate_cn($string,$length=0,$ellipsis='…',$start=0){ $string=strip_tags($string); $string=preg_replace('/\n/is','',$string); //$string=preg_replace('/ | /is','',$string);//清除字符串中的空格 $string=preg_replace('/ /is','',$string); preg_match_all("/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/",$string,$string); if(is_array($string)&&!empty($string[0])){ $string=implode('',$string[0]); if(strlen($string)<$start+1){ return ''; } preg_match_all("/./su",$string,$ar); $string2=''; $tstr=''; //www.phpernote.com for($i=0;isset($ar[0][$i]);$i++){ if(strlen($tstr)<$start){ $tstr.=$ar[0][$i]; }else{ if(strlen($string2)<$length+strlen($ar[0][$i])){ $string2.=$ar[0][$i]; }else{ break; } } } return $string==$string2?$string2:$string2.$ellipsis; }else{ $string=''; } return $string; }
(3)You can use this plug-in below. In the future, you can directly use the truncate_cn function in the template to intercept Chinese strings, such as: {$news.content|truncate_cn:' 30'}
At this point, a plug-in for the php smarty template is completed. Isn’t it very simple? I hope you can learn and build your own smarty into a more personalized template engine.