Make your Smarty smarter

Source: Internet
Author: User
Tags php compiler
Name the above code & quot; function. page. php & quot; and save it to the Smarty plugins directory. 1. expand your Smarty
1. preparation effort

PHP Code:--------------------------------------------------------------------------------
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 = $ 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 "function. page. php" and save it to the Plugin directory of Smarty.


Code :--------------------------------------------------------------------------------
HTMLPUBLIC "-// W3C // dtd html 4.0 Transitional // EN">


New Document







{Page row = 10}
{Page row = 10 now = 5}
{Page row = 10 now = 5 href = "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 = "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 = "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 = "plugins. php? A = 1 & B = 2 & page = "frist =" first page "prev =" previous page "next =" next page "last =" last page "}


--------------------------------------------------------------------------------


Name the above code "plugins.html" and save it to the template directory of Smarty.

2. test procedures

PHP code :--------------------------------------------------------------------------------
$ Smarty-> display ('plugins.html ');

--------------------------------------------------------------------------------


3. Instructions for use
I am too lazy to compare the usage of five {pages} in "plugins.html" and check the displayed results to understand what it is.

4. plug-ins
"Smarty Manual Chapter 16th. extension of Smarty with plug-ins" applications. 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 :--------------------------------------------------------------------------------
/**
*
*/
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
This is translation from the village. In fact, the application of Smarty 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 ZendAccelerator (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. As a result, no syntax parsing is spent on the template file, and each template can enjoy something like a Zend accelerator (http://www.zend.com) or a PHP accelerator (http://www.php-accelerator.co.uk ). This php compiler high-speed cache solution.

Some of Smarty's features:
Some features of Smaty:

It is extremely fast.
Very fast!

It is efficient since the PHP parser does the dirty work.
Using a php analyzer to perform this task is effective.

No template parsing overhead, only compiles once.
No extra template syntax parsing is required, and only one compilation is required.

It is smart about recompiling only the template files that have changed.
Only recompile the modified template file

You can make custom functions and custom variable modifiers, so the template language is extremely extensible.
You can edit 'udf 'and 'variable'. Therefore, this template language can be fully expanded.

Retriable 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 the {if...} expression is simple or composite, as you like.

Unlimited nesting of sections, ifs, etc. allowed.
If allowed, sections can be nested infinitely.

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
Customizable cache processing functions

Plugin architecture
Plug-in architecture

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.