溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么在php中使用smarty修飾變量

發(fā)布時間:2021-04-02 15:48:41 來源:億速云 閱讀:125 作者:Leah 欄目:開發(fā)技術

這篇文章將為大家詳細講解有關怎么在php中使用smarty修飾變量,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。

test.php代碼:

<?php 
require 'libs/Smarty.class.php'; //包含Smarty類庫文件 
$smarty = new Smarty; //創(chuàng)建一個新的Smarty對象 
$total = 12345; //對$total賦值 
$smarty->assign("total",$total); //對模版中的變量賦值 
$formatted_total = number_format($total); //格式化$total 
$smarty->assign("formatted_total",$formatted_total); //對模版中的變量賦值
$smarty->display('test1.htm'); //顯示頁面 
?>

test1.html模板代碼:

<html> 
  <head> 
    <title>Smarty Test</title> 
  </head> 
  <body> 
    <H1>Total is {$total}</H1> 
    <H1>Formatted Total is {$formatted_total}</H1> 
  </body> 
</html>

編譯后的test.html.php代碼:

<?php /* Smarty version 2.6.22, created on 2009-03-19 14:37:39 
     compiled from test1.htm */ ?> 
<html> 
  <head> 
    <title>Smarty Test</title> 
  </head> 
  <body> 
    <H1>Total is <?php echo $this->_tpl_vars['total']; ?> 
</H1> 
    <H1>Formatted Total is <?php echo $this->_tpl_vars['formatted_total']; ?> 
</H1> 
  </body> 
</html>

test1.html模板可以改寫成這樣test2.html:

<html> 
  <head> 
    <title>Smarty Test</title> 
  </head> 
  <body> 
    <H1>Total is {$total}</H1> 
    <H1>Formatted Total is {$total|number_format}</H1> 
  </body> 
</html>

則相應的test.php代碼改為:

<?php 
require 'libs/Smarty.class.php'; //包含Smarty類庫文件
$smarty = new Smarty; //創(chuàng)建一個新的Smarty對象 
$total = 12345; 
$smarty->assign("total",$total); //對模版中的變量賦值 
$smarty->display('test2.htm'); //顯示頁面 
?>

瀏覽器顯示:

Total is 12345
Formatted Total is 12,345

關于怎么在php中使用smarty修飾變量就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。

AI