首頁 後端開發 php教程 让你的Smarty更聪明_PHP

让你的Smarty更聪明_PHP

Jun 01, 2016 pm 12:34 PM
from href page pages smarty

SMARTY

一、扩展你的Smarty
1、准备功夫
<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>
登入後複製


将上面的代码命名为"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>
登入後複製


将上面的代码命名为"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>
登入後複製



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>
登入後複製


Smarty的功能很多很多(使用了半年,深有体会),还有待大家继续发掘。。。。

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1664
14
CakePHP 教程
1421
52
Laravel 教程
1315
25
PHP教程
1266
29
C# 教程
1239
24
src和href是什麼意思 src和href是什麼意思 Aug 16, 2023 pm 05:00 PM

src和href分別是,1、src是source的縮寫,用來指定外部資源的路徑,通常用於嵌入外部文件,例如圖片、音訊、視訊等,src屬性一般用在img、script、iframe等標籤上;2、href是hypertext reference的縮寫,用於指定超連結的目標資源的路徑,通常用於連結到外部文件或其他頁面,href屬性一般用在a、link等標籤上。

Vue專案中如何實現資料的分頁與顯示最佳化 Vue專案中如何實現資料的分頁與顯示最佳化 Oct 15, 2023 am 09:27 AM

Vue專案中實現資料的分頁和顯示優化在Vue專案中,當頁面需要展示大量資料時,通常需要進行資料的分頁和顯示最佳化以提高使用者體驗,本文將介紹如何使用Vue實現資料的分頁和顯示最佳化,並提供具體的程式碼範例。一、資料分頁資料分頁是指將大量資料依照一定的規則分割成多頁,並在頁面上進行分頁顯示。 Vue專案中可以使用下列步驟來實現資料分頁:定義資料來源首先,定義一個包含所有數

href和src發送的什麼請求 href和src發送的什麼請求 Aug 17, 2023 pm 02:20 PM

href和src發送的get請求。詳細說明:1、href屬性,用於指定連結的目標資源,引用外部樣式表會發送GET請求來獲取CSS文件,引用文檔會發送GET請求來獲取指定的HTML文件,引用圖像時它會發送GET請求來獲取指定的圖像檔案;2、src屬性,用於指定嵌入資源的URL,引用圖像時會發送GET請求來獲取指定的圖像文件,引用音頻它會發送一個GET請求來獲取指定的音頻文件等等。

PHP中的模板引擎Smarty初探 PHP中的模板引擎Smarty初探 May 11, 2023 pm 05:15 PM

現如今,網站的開發離不了一個重要的組成部分——模板引擎。模板引擎是指一種將頁面模板和資料結合起來產生具有特定格式的html程式碼的工具。在各種網站開發框架中,模板引擎是一個必不可少的元件,因為模板引擎可以大量減少程式碼的重複性和提高頁面的動態性。其中一個最常見且流行的模板引擎是Smarty。 Smarty是基於PHP語言開發的DSL(DomainSpecif

You can't specify target table 'table_name' for update in FROM clause - 如何解決MySQL報錯:無法在FROM子句中更新目標表 You can't specify target table 'table_name' for update in FROM clause - 如何解決MySQL報錯:無法在FROM子句中更新目標表 Oct 05, 2023 am 10:25 AM

你好,以下是一篇1500字以內的文章,標題為:Youcan'tspecifytargettable'table_name'forupdateinFROMclause-如何解決MySQL報錯:無法在FROM子句中更新目標表,需要具體程式碼範例。在進行MySQL資料庫開發過程中,我們有時會遇到以下報錯資訊:Youcan'tspecify

src屬性和href屬性在功能和用法上的差異有哪些? src屬性和href屬性在功能和用法上的差異有哪些? Dec 28, 2023 am 08:20 AM

src屬性和href屬性是在HTML中常用的屬性,用來載入外部資源。雖然它們有相似的目的,但在使用和用途上有一些不同。 src屬性:src屬性用於指定要在文件中嵌入的外部資源,主要用於在HTML文件中引入外部腳本文件和媒體文件。它可以用於以下幾種情況:引入外部JavaScript檔案:透過src屬性,將外部的JavaScript檔案連結到HTML頁面。

如何使用PHP和Smarty實現前後端分離開發 如何使用PHP和Smarty實現前後端分離開發 Jun 25, 2023 pm 01:46 PM

在現代web開發中,前後端分離已經成為了一個非常流行的趨勢,它能夠讓開發者更好地組織專案並且提高了專案開發的效率。 PHP和Smarty是兩個非常常用的技術,它們可以用來實現前後端分離的開發方式。本文將會介紹如何使用PHP和Smarty來實現前後端分離開發。什麼是前後端分離開發在傳統的web開發中,前端主要負責頁面的呈現以及與後端互動的邏輯。後端則主要負責業

src和href的差別究竟是什麼?快速了解! src和href的差別究竟是什麼?快速了解! Jan 06, 2024 pm 09:09 PM

src和href的差別究竟是什麼?快速了解!在網頁開發過程中,src和href是兩個常用的屬性。儘管它們看起來類似,但實際上有著不同的用途和適用場景。在本文中,我們將深入探討src和href的區別,並透過具體的程式碼範例來解釋它們。在HTML中,src是用來指定要嵌入或引用的外部資源的屬性,它通常用於引用映像、音訊、視訊或腳本檔案。與此不同,href是超鏈

See all articles