Phpsmarty Chinese interception plug-in development example _ PHP Tutorial

Source: Internet
Author: User
Phpsmarty is an example of Chinese interception plug-in development. 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. The smarty is undoubtedly the most popular and famous template engine in php development. using this template engine brings great convenience to our development work. Next, let's share the smarty plug-in technology (taking the creation of a php smarty Chinese string truncation as an example), and make full use of the various features of smarty to make php smarty a sharp weapon in our hands, this makes 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 PHP template engine written in PHP and a template system recommended by php.net.
 
2. what is the smarty plug-in?

The smarty plug-in refers to the plug-ins in smarty, which are some functional control statements embedded in the template. the Variable Modifiers in smarty is actually some built-in plug-ins.

3. how does the plug-in work?

The smarty template uses the plug-in call statement to dynamically load the file. you can put the plug-in you have written into the plugins directory under the lib directory in the smarty directory, in this way, these plug-ins will be automatically loaded when they are used in the template.

4. what types of plug-ins are available?

Smarty plug-ins are of the following types: function, modifier, block, compiler, prefilter, postfilter, outputfilter, resource, and insert. in this article, we will only share how to develop function plug-ins, other types of development methods are similar, so you can try them.

5. how to name the plug-in?

File name format:

Type. name. php

Type refers to the type. the above mentioned types are its selection range;

Name: Custom plug-in name. this document uses showNews to name it;

Function name:

Smarty_type_name () smarty: a fixed name at a fixed position; type is consistent with the type of the file name, and name is consistent with the name in the file name

(2) the basic knowledge is clear, and the development is started below. Copy the following code to the file and 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. you can use it based on your own situation, but it must be in the plugins directory ).

/** Author: http://www.phpernote.com/*January 31, 2013:, August 1,. screenshots of the Chinese character string: functions: 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); // clear the spaces in the 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_a Rray ($ string )&&! Empty ($ string [0]) {$ string = implode ('', $ string [0]); if (strlen ($ string) <$ start + 1) {return '';} preg_match_all ("/. /su ", $ string, $ ar); $ string2 =''; $ tstr = ''; // www. phpernote. comfor ($ 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) The plug-in can be used below. in the future, you can directly use the truncate_cn function in the template to intercept Chinese strings, for example: {$ news. content | truncate_cn: '30 '}

So far, a php smarty template plug-in has been completed. is it very simple? I hope you can learn and build your smarty into a more personalized template engine.

Articles you may be interested in
  • Use php functions in the smarty template and how to use multiple functions for a variable in the smarty Template
  • Extension of the for loop in the smarty Template
  • How does php clear html format, remove spaces in text, and intercept text?
  • PHP method for obtaining file extension (suffix)
  • Solution to Smarty temporary file creation failure
  • Use the PHP function memory_get_usage to obtain the current PHP memory consumption for program performance optimization.
  • Jquery pop-up window plug-in (compatible with all browsers)
  • Linux chmod (file or folder permission setting) command parameters and usage details

Http://www.bkjia.com/PHPjc/764122.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/764122.htmlTechArticlesmarty is undoubtedly php development inside the most popular and most famous template engine, through the use of the template engine, to our development work has brought great convenience. Share the following...

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.