Make your Smarty smarter _ PHP Tutorial

Source: Internet
Author: User
Tags php compiler
Make your Smarty smarter. 1. expand your Smarty1. prepare the Kung Fu PHP code: javasfunctionsmarty_function_page ($ params, $ space 1. expand your Smarty 1. prepare the Kung Fu PHP code: using function Smarty_function_page ($ params, & $ Smarty) {$ href =#; $ space =; $ frist = NULL; $ last = NULL; $ page = 5; extract ($ params); if (! $ Row | $ row <= 1) return; $ pages = $ row; $ curr_page = $ now? $ Now: 1; $ offset = 2; $ from = $ curr_page-$ offset; $ to = $ curr_page + $ page-$ offset-1; if ($ page >$ pages) {$ from = 1; $ to = $ pages;} else {if ($ from <1) {$ to = $ curr_page + 1-$ from; $ from = 1; if ($ to-$ from) <$ page & ($ to-$ from) <$ pages) {$ to = $ page ;}} elseif ($ to> $ pages) {$ from = $ curr_page-$ pages + $ to; $ to = $ pages; if ($ to-$ from) <$ page & ($ to-$ from) <$ pages) {$ from = $ pages-$ page + 1 ;}}} if ($ frist & ($ curr_page-1)> = 1) $ p [frist] =. $ frist .; if ($ prev & ($ I = $ curr_page-1)> = 1) $ p [prev] =. $ prev .; for ($ I = $ from; $ I <= $ to; $ I ++) {if ($ I = $ curr_page) {$ p [$ I] = [. $ I.];} else {$ p [$ I] =. $ I .;}} if ($ next & ($ I = $ curr_page + 1) <= $ pages) $ p [next] =. $ next .; if ($ last & ($ curr_page + 1) <= $ pages) $ p [last] =. $ last .; return implode ($ space, $ p);} // end func ---------------------------------------------------------------------------- name the above code as "function. page. php "code saved to the Smarty plugins directory :-------------------------------------------------------------------------------- New Document {Page row = 10} {page row = 10 now = 5} {page row = 10 now = 5 href = DownloadFilesa2004-11-02 "plugins. php $ a = 1 & B = 2 & page = "frist =" first page "prev =" previous page "next =" next page "last =" last page "} {page row = 10 now = 5 href = DownloadFilesa2004-11-02 "plugins. php $ a = 1 & B = 2 & page = "frist =" first page "prev =" previous page "next =" next page "last =" last page "} {page row = 10 now = 1 href = DownloadFilesa2004-11-02 "plugins. php $ a = 1 & B = 2 & page = "frist =" first page "prev =" previous page "next =" next page "last =" last page "} {page row = 10 now = 10 href = DownloadFilesa2004-11-02 "plugins. php $ a = 1 & B = 2 & page = "frist =" first page "prev =" previous page "next =" next page "last =" last page "} Compile name the above code as "plugins.html" and save it to the template directory of Smarty. 2. test program PHP code: Prepare $ Smarty-> display (plugins.html ); 3. I am too lazy to explain how to use the plug-in. compare the usage of five {pages} in "plugins.html" and check the displayed results to understand what the plug-in is. 4. Describes the application of "Smarty Manual Chapter 16th. extension of Smarty with plug-ins. You can use plugins to expand Smarty for Chinese character interceptions. the built-in Smarty interceptions do not support Chinese characters. ______________ II. Smarty automatically generates static pages. if your file extension is ". html "~~~~~ Hey, isn't this a static page? -_-! How to obtain static file names? PHP code: samples/*****/class template extends Smarty {/*****/function template () {$ this-> Smarty ();} // end func/*****/function name ($ tpl_file, $ cache_id = null, $ compile_id = null) {if (! Isset ($ compile_id) $ compile_id = $ this-> compile_id; $ _ auto_id = $ this-> _ get_auto_id ($ cache_id, $ compile_id ); $ _ cache_file = $ this-> _ get_auto_filename ($ this-> cache_dir, $ tpl_file, $ _ auto_id); return basename ($ _ cache_file );} // end func} // end class $ Smarty = new template; $ file_name = $ Smarty-> name (plugins.html, cache_name); # name of the html file (excluding the path) $ Smarty-> cache_lifetime =-1; # static files never expire $ Smarty -> Fetch (plugins.html, cache_name); # generate a static html file, which is translated by the village. In fact, the Smarty application is far more than that. One of the unique aspects about Smarty is the template compling. this means Smarty reads the template files and creates PHP scripts from them. once they are created, they are executed from then on. therefore there is no costly template file parsing for each request, and each template can take full advantage of PHP compiler cache solutions such as Zend Accelerator ( http://www.zend.com ) Or PHP Accelerator ( http://www.php-accelerator.co.uk ). One of the features of Smarty is "template compilation ". It means that Smarty reads the template file and uses it to create a php script. These scripts will be executed after they are created. Therefore, the syntax parsing of the template file is not spent, and each template can enjoy ( http://www.zend.com ) Or PHP accelerator ( http://www.php-accelerator.co.uk ). This php compiler high-speed cache solution. Some of the features of Smartys features: Smaty: It is extremely fast. very fast! It is efficient since the PHP parser does the dirty work. using the php analyzer to do this task is a valid No template parsing overhead, only compiles once. no extra template syntax parsing is required, just compile It once is smart about recompiling only the template files that have changed. you can make M functions and custom variable modifiers, so the template language is extremely extensible. you can edit user-defined functions and user-defined variables. Therefore, this template language can fully extend the Extends able template delimiter tag syntax, so you can use {},{{}}, , Etc. you can set template delimiters by yourself, so you can use {},{{}}, , Etc. The if/elseif/else/endif constructs are passed to the PHP parser, so the {if ...} expression syntax can be as simple or as complex as you like. for example, if/elseif/else/endif statements can be passed to the php syntax parser, so {if ...} the expression is simple or composite, and you like Unlimited nesting of sections, ifs, etc. allowed. if allowed, It is possible to embed PHP code right in your template files, although this may not be needed (nor recommended) since the engine is so customizable. the engine can be customized. php code can be embedded into your template file, although this may not be required (not recommended) built-in caching support built-in cache support Arbitrary template sources Independent template file Custom cache handling functions you can customize the cache processing function in architecture plug-in architecture

Step 1. prepare the Kung Fu PHP code: ---------------------------------------------------------------------------- function Smarty_function_page ($ params, $ space...

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.