让你的Smarty更聪明_PHP
SMARTY
一、扩展你的Smarty1、准备功夫
<font size="2"><font face="Verdana">PHP代码:</font><hr></font><code><font color="#000000"> <font color="#0000bb"><br></font><font color="#007700">function </font><font color="#0000bb">Smarty_function_page </font><font color="#007700">( </font><font color="#0000bb">$params</font><font color="#007700">, &</font><font color="#0000bb">$Smarty </font><font color="#007700">) <br>{ <br> </font><font color="#0000bb">$href </font><font color="#007700">= </font><font color="#dd0000">'#'</font><font color="#007700">; <br> </font><font color="#0000bb">$space </font><font color="#007700">= </font><font color="#dd0000">' '</font><font color="#007700">; <br> <br> </font><font color="#0000bb">$frist </font><font color="#007700">= </font><font color="#0000bb">NULL</font><font color="#007700">; <br> </font><font color="#0000bb">$last </font><font color="#007700">= </font><font color="#0000bb">NULL</font><font color="#007700">; <br> <br> </font><font color="#0000bb">$page </font><font color="#007700">= </font><font color="#0000bb">5</font><font color="#007700">; <br> <br> </font><font color="#0000bb">extract</font><font color="#007700">(</font><font color="#0000bb">$params</font><font color="#007700">); <br> <br> if ( !</font><font color="#0000bb">$row </font><font color="#007700">|| </font><font color="#0000bb">$row </font><font color="#007700"><font color="#0000bb">1 </font><font color="#007700">) return </font><font color="#dd0000">' '</font><font color="#007700">; <br> <br> </font><font color="#0000bb">$pages </font><font color="#007700">= </font><font color="#0000bb">$row</font><font color="#007700">; <br> </font><font color="#0000bb">$curr_page </font><font color="#007700">= </font><font color="#0000bb">$now </font><font color="#007700">? </font><font color="#0000bb">$now </font><font color="#007700">: </font><font color="#0000bb">1</font><font color="#007700">; <br> </font><font color="#0000bb">$offset </font><font color="#007700">= </font><font color="#0000bb">2</font><font color="#007700">; <br> </font><font color="#0000bb">$from </font><font color="#007700">= </font><font color="#0000bb">$curr_page </font><font color="#007700">- </font><font color="#0000bb">$offset</font><font color="#007700">; <br> </font><font color="#0000bb">$to </font><font color="#007700">= </font><font color="#0000bb">$curr_page </font><font color="#007700">+ </font><font color="#0000bb">$page </font><font color="#007700">- </font><font color="#0000bb">$offset </font><font color="#007700">- </font><font color="#0000bb">1</font><font color="#007700">; <br> <br> if ( </font><font color="#0000bb">$page </font><font color="#007700">> </font><font color="#0000bb">$pages </font><font color="#007700">) <br> { <br> </font><font color="#0000bb">$from </font><font color="#007700">= </font><font color="#0000bb">1</font><font color="#007700">; <br> </font><font color="#0000bb">$to </font><font color="#007700">= </font><font color="#0000bb">$pages</font><font color="#007700">; <br> } <br> else <br> { <br> if ( </font><font color="#0000bb">$from </font><font color="#007700"><font color="#0000bb">1</font><font color="#007700">) <br> { <br> </font><font color="#0000bb">$to </font><font color="#007700">= </font><font color="#0000bb">$curr_page </font><font color="#007700">+ </font><font color="#0000bb">1 </font><font color="#007700">- </font><font color="#0000bb">$from</font><font color="#007700">; <br> </font><font color="#0000bb">$from </font><font color="#007700">= </font><font color="#0000bb">1</font><font color="#007700">; <br> if ( ( </font><font color="#0000bb">$to </font><font color="#007700">- </font><font color="#0000bb">$from </font><font color="#007700">) <font color="#0000bb">$page </font><font color="#007700">&& ( </font><font color="#0000bb">$to </font><font color="#007700">- </font><font color="#0000bb">$from </font><font color="#007700">) <font color="#0000bb">$pages </font><font color="#007700">) <br> { <br> </font><font color="#0000bb">$to </font><font color="#007700">= </font><font color="#0000bb">$page</font><font color="#007700">; <br> } <br> } <br> elseif ( </font><font color="#0000bb">$to </font><font color="#007700">> </font><font color="#0000bb">$pages </font><font color="#007700">) <br> { <br> </font><font color="#0000bb">$from </font><font color="#007700">= </font><font color="#0000bb">$curr_page </font><font color="#007700">- </font><font color="#0000bb">$pages </font><font color="#007700">+ </font><font color="#0000bb">$to</font><font color="#007700">; <br> </font><font color="#0000bb">$to </font><font color="#007700">= </font><font color="#0000bb">$pages</font><font color="#007700">; <br> if ( ( </font><font color="#0000bb">$to </font><font color="#007700">- </font><font color="#0000bb">$from </font><font color="#007700">) <font color="#0000bb">$page </font><font color="#007700">&& ( </font><font color="#0000bb">$to </font><font color="#007700">- </font><font color="#0000bb">$from</font><font color="#007700">) <font color="#0000bb">$pages </font><font color="#007700">) <br> { <br> </font><font color="#0000bb">$from </font><font color="#007700">= </font><font color="#0000bb">$pages </font><font color="#007700">- </font><font color="#0000bb">$page </font><font color="#007700">+ </font><font color="#0000bb">1</font><font color="#007700">; <br> } <br> } <br> } <br> <br> if ( </font><font color="#0000bb">$frist </font><font color="#007700">&& ( </font><font color="#0000bb">$curr_page </font><font color="#007700">- </font><font color="#0000bb">1 </font><font color="#007700">) >= </font><font color="#0000bb">1 </font><font color="#007700">) </font><font color="#0000bb">$p</font><font color="#007700">[</font><font color="#dd0000">'frist'</font><font color="#007700">] = </font><font color="#dd0000">'<a href="'%20</font><font%20color=">. </a></font><font color="#0000bb">$href </font><font color="#007700">. </font><font color="#dd0000">'1">' </font><font color="#007700">. </font><font color="#0000bb">$frist </font><font color="#007700">. </font><font color="#dd0000">''</font><font color="#007700">; <br> if ( </font><font color="#0000bb">$prev </font><font color="#007700">&& ( </font><font color="#0000bb">$i </font><font color="#007700">= </font><font color="#0000bb">$curr_page </font><font color="#007700">- </font><font color="#0000bb">1 </font><font color="#007700">) >= </font><font color="#0000bb">1 </font><font color="#007700">) </font><font color="#0000bb">$p</font><font color="#007700">[</font><font color="#dd0000">'prev'</font><font color="#007700">] = </font><font color="#dd0000">'<a href="'%20</font><font%20color=">. </a></font><font color="#0000bb">$href </font><font color="#007700">. </font><font color="#0000bb">$i </font><font color="#007700">. </font><font color="#dd0000">'">' </font><font color="#007700">. </font><font color="#0000bb">$prev </font><font color="#007700">. </font><font color="#dd0000">''</font><font color="#007700">; <br> for( </font><font color="#0000bb">$i </font><font color="#007700">= </font><font color="#0000bb">$from</font><font color="#007700">; </font><font color="#0000bb">$i </font><font color="#007700"><font color="#0000bb">$to</font><font color="#007700">; </font><font color="#0000bb">$i </font><font color="#007700">++ ) <br> { <br> if ( </font><font color="#0000bb">$i </font><font color="#007700">== </font><font color="#0000bb">$curr_page </font><font color="#007700">) <br> { <br> </font><font color="#0000bb">$p</font><font color="#007700">[</font><font color="#0000bb">$i</font><font color="#007700">] = </font><font color="#dd0000">'<a href="'%20</font><font%20color=">. </a></font><font color="#0000bb">$href </font><font color="#007700">. </font><font color="#0000bb">$i </font><font color="#007700">. </font><font color="#dd0000">'" class="nowpage" target="_self">[' </font><font color="#007700">. </font><font color="#0000bb">$i </font><font color="#007700">. </font><font color="#dd0000">']'</font><font color="#007700">; <br> } <br> else <br> { <br> </font><font color="#0000bb">$p</font><font color="#007700">[</font><font color="#0000bb">$i</font><font color="#007700">] = </font><font color="#dd0000">'<a href="'%20</font><font%20color=">. </a></font><font color="#0000bb">$href </font><font color="#007700">. </font><font color="#0000bb">$i </font><font color="#007700">. </font><font color="#dd0000">'" target="_self">' </font><font color="#007700">. </font><font color="#0000bb">$i </font><font color="#007700">. </font><font color="#dd0000">''</font><font color="#007700">; <br> } <br> } <br> if ( </font><font color="#0000bb">$next </font><font color="#007700">&& ( </font><font color="#0000bb">$i </font><font color="#007700">= </font><font color="#0000bb">$curr_page </font><font color="#007700">+ </font><font color="#0000bb">1 </font><font color="#007700">) <font color="#0000bb">$pages </font><font color="#007700">) </font><font color="#0000bb">$p</font><font color="#007700">[</font><font color="#dd0000">'next'</font><font color="#007700">] = </font><font color="#dd0000">'<a href="'%20</font><font%20color=">. </a></font><font color="#0000bb">$href </font><font color="#007700">. </font><font color="#0000bb">$i </font><font color="#007700">. </font><font color="#dd0000">'" target="_self">' </font><font color="#007700">. </font><font color="#0000bb">$next </font><font color="#007700">. </font><font color="#dd0000">''</font><font color="#007700">; <br> if ( </font><font color="#0000bb">$last </font><font color="#007700">&& ( </font><font color="#0000bb">$curr_page </font><font color="#007700">+ </font><font color="#0000bb">1 </font><font color="#007700">) <font color="#0000bb">$pages </font><font color="#007700">) </font><font color="#0000bb">$p</font><font color="#007700">[</font><font color="#dd0000">'last'</font><font color="#007700">] = </font><font color="#dd0000">'<a href="'%20</font><font%20color=">. </a></font><font color="#0000bb">$href </font><font color="#007700">. </font><font color="#0000bb">$pages </font><font color="#007700">. </font><font color="#dd0000">'" target="_self">' </font><font color="#007700">. </font><font color="#0000bb">$last </font><font color="#007700">. </font><font color="#dd0000">''</font><font color="#007700">; <br> <br> return </font><font color="#0000bb">implode</font><font color="#007700">( </font><font color="#0000bb">$space</font><font color="#007700">, </font><font color="#0000bb">$p </font><font color="#007700">); <br>} </font><font color="#ff8000">// end func<br></font><font color="#0000bb"></font> </font> </font></font></font></font></font></font></font></font></font></code><hr>Copy after login
将上面的代码命名为"function.page.php"保存到Smarty的plugins目录里
<font size="2"><font face="verdana,arial,helvetica">代码:</font><hr></font> <title> New Document </title> <meta name="Generator" content="EditPlus"> <meta name="Author" content=""> <meta name="Keywords" content=""> <meta name="Description" content=""> {page row=10} {page row=10 now=5} {page row=10 now=5 href="plugins.php?a=1&b=2&page=" frist="第一页" prev="上一页" next="下一页" last="最后页"} {page row=10 now=5 href="plugins.php?a=1&b=2&page=" frist="第一页" prev="上一页" next="下一页" last="最后页"} {page row=10 now=1 href="plugins.php?a=1&b=2&page=" frist="第一页" prev="上一页" next="下一页" last="最后页"} {page row=10 now=10 href="plugins.php?a=1&b=2&page=" frist="第一页" prev="上一页" next="下一页" last="最后页"} <hr>Copy after login
将上面的代码命名为"plugins.html"保存到Smarty的template目录里
2、测试程序
<font size="2"><font face="verdana,arial,helvetica">PHP代码:</font><hr></font><code><font color="#000000"> <font color="#0000bb"><br>$Smarty</font><font color="#007700">-></font><font color="#0000bb">display</font><font color="#007700">( </font><font color="#dd0000">'plugins.html' </font><font color="#007700">);<br></font><font color="#0000bb"></font> </font> </code><hr>Copy after login
3、使用说明
我懒得打了,对比一下"plugins.html"的5个{page}用法,以及看看显示出来的效果就明白是什么了
4、插件说明
“《Smarty手册》第十六章.以插件扩展Smarty ”的应用。像中文字符截取之类的都可以以plugins扩展Smarty,Smarty自带的截取不支持中文。
二、Smarty自动生成静态页面
如果你的文件扩展名为".html"~~~~~嘿嘿,这不就是静态页面了吗?-_-!
至于怎么取得静态的文件名呢?
<font size="2"><font face="verdana,arial,helvetica">PHP代码:</font><hr></font><code><font color="#000000"> <font color="#0000bb"><br></font><font color="#ff8000">/** <br> * <br> */ <br></font><font color="#007700">class </font><font color="#0000bb">template </font><font color="#007700">extends </font><font color="#0000bb">Smarty <br></font><font color="#007700">{ <br> <br> </font><font color="#ff8000">/** <br> * <br> */ <br> </font><font color="#007700">function </font><font color="#0000bb">template </font><font color="#007700">() <br> { <br> </font><font color="#0000bb">$this</font><font color="#007700">-></font><font color="#0000bb">Smarty</font><font color="#007700">(); <br> } </font><font color="#ff8000">// end func <br> <br> /** <br> * <br> */ <br> </font><font color="#007700">function </font><font color="#0000bb">name </font><font color="#007700">( </font><font color="#0000bb">$tpl_file</font><font color="#007700">, </font><font color="#0000bb">$cache_id </font><font color="#007700">= </font><font color="#0000bb">null</font><font color="#007700">, </font><font color="#0000bb">$compile_id </font><font color="#007700">= </font><font color="#0000bb">null </font><font color="#007700">) <br> { <br> <br> if (!isset(</font><font color="#0000bb">$compile_id</font><font color="#007700">)) </font><font color="#0000bb">$compile_id </font><font color="#007700">= </font><font color="#0000bb">$this</font><font color="#007700">-></font><font color="#0000bb">compile_id</font><font color="#007700">; <br> <br> </font><font color="#0000bb">$_auto_id </font><font color="#007700">= </font><font color="#0000bb">$this</font><font color="#007700">-></font><font color="#0000bb">_get_auto_id</font><font color="#007700">( </font><font color="#0000bb">$cache_id</font><font color="#007700">, </font><font color="#0000bb">$compile_id </font><font color="#007700">); <br> </font><font color="#0000bb">$_cache_file </font><font color="#007700">= </font><font color="#0000bb">$this</font><font color="#007700">-></font><font color="#0000bb">_get_auto_filename</font><font color="#007700">( </font><font color="#0000bb">$this</font><font color="#007700">-></font><font color="#0000bb">cache_dir</font><font color="#007700">, </font><font color="#0000bb">$tpl_file</font><font color="#007700">, </font><font color="#0000bb">$_auto_id </font><font color="#007700">); <br> <br> return </font><font color="#0000bb">basename</font><font color="#007700">( </font><font color="#0000bb">$_cache_file </font><font color="#007700">); <br> } </font><font color="#ff8000">// end func <br></font><font color="#007700">} </font><font color="#ff8000">// end class <br> <br></font><font color="#0000bb">$Smarty </font><font color="#007700">= new </font><font color="#0000bb">template</font><font color="#007700">; <br></font><font color="#0000bb">$file_name </font><font color="#007700">= </font><font color="#0000bb">$Smarty</font><font color="#007700">-></font><font color="#0000bb">name</font><font color="#007700">( </font><font color="#dd0000">'plugins.html'</font><font color="#007700">, </font><font color="#dd0000">'cache_name' </font><font color="#007700">);</font><font color="#ff8000">#html文件的名字(不包含路径) <br> <br></font><font color="#0000bb">$Smarty</font><font color="#007700">-></font><font color="#0000bb">cache_lifetime </font><font color="#007700">= -</font><font color="#0000bb">1</font><font color="#007700">;</font><font color="#ff8000">#静态文件永不过期 <br></font><font color="#0000bb">$Smarty</font><font color="#007700">-></font><font color="#0000bb">fetch</font><font color="#007700">( </font><font color="#dd0000">'plugins.html'</font><font color="#007700">, </font><font color="#dd0000">'cache_name' </font><font color="#007700">);</font><font color="#ff8000">#生成静态html文件<br></font><font color="#0000bb"></font> </font> </code><hr>Copy after login
Smarty的功能很多很多(使用了半年,深有体会),还有待大家继续发掘。。。。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



src and href are respectively, 1. src is the abbreviation of source, which is used to specify the path of external resources. It is usually used to embed external files, such as pictures, audios, videos, etc. The src attribute is generally used on img, script, iframe and other tags. ; 2. href is the abbreviation of hypertext reference, which is used to specify the path of the target resource of the hyperlink. It is usually used to link to external documents or other pages. The href attribute is generally used on tags such as a and link.

Implementing data paging and display optimization in Vue projects. In Vue projects, when a page needs to display a large amount of data, data paging and display optimization usually need to be performed to improve user experience. This article will introduce how to use Vue to implement data paging and display optimization. , and provide specific code examples. 1. Data paging Data paging refers to dividing a large amount of data into multiple pages according to certain rules and displaying them on the page. You can use the following steps to implement data paging in a Vue project: Define the data source. First, define a

The src attribute and href attribute are commonly used attributes in HTML and are used to load external resources. Although they have similar purposes, there are some differences in usage and purpose. src attribute: The src attribute is used to specify external resources to be embedded in the document. It is mainly used to introduce external script files and media files into HTML documents. It can be used in the following situations: Introduce external JavaScript files: Link external JavaScript files to HTML pages through the src attribute.

Get requests sent by href and src. Detailed description: 1. The href attribute is used to specify the target resource of the link. When referencing an external style sheet, it will send a GET request to obtain the CSS file. When referencing the document, it will send a GET request to obtain the specified HTML file. When referencing an image, it will send a GET request. To obtain the specified image file; 2. The src attribute is used to specify the URL of the embedded resource. When referencing the image, it will send a GET request to obtain the specified image file. When referencing the audio, it will send a GET request to obtain the specified audio file, etc. .

Nowadays, website development is inseparable from an important component-template engine. A template engine refers to a tool that combines page templates and data to generate HTML code with a specific format. In various website development frameworks, the template engine is an essential component, because the template engine can greatly reduce the duplication of code and improve the dynamics of the page. One of the most common and popular template engines is Smarty. Smarty is a DSL (DomainSpecif

In modern web development, the separation of front-end and back-end has become a very popular trend, which allows developers to better organize projects and improve the efficiency of project development. PHP and Smarty are two very commonly used technologies, which can be used to achieve front-end and back-end separation development. This article will introduce how to use PHP and Smarty to achieve front-end and back-end separation development. What is front-end and back-end separation development? In traditional web development, the front-end is mainly responsible for the presentation of the page and the logic of interaction with the back-end. The backend is mainly responsible for the business

Hello, the following is an article within 1500 words, titled: Youcan'tspecifytargettable'table_name'forupdateinFROMclause-How to solve the MySQL error: Unable to update the target table in the FROM clause, specific code examples are needed. During the development of MySQL database, we sometimes encounter the following error message: Youcan'tspecify

As a PHP developer, using a template engine is a natural choice. Smarty is a popular template engine that provides a way to separate HTML/CSS/JavaScript from PHP code, allowing developers to better organize and manage projects. In this article, we will learn how to use Smarty template engine during PHP development. 1. Install Smarty Before, we must install Smarty. In this article we will use Composer to install
