温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在php中使用smarty修饰变量

发布时间:2021-04-02 15:48:41 来源:亿速云 阅读:120 作者:Leah 栏目:开发技术

这篇文章将为大家详细讲解有关怎么在php中使用smarty修饰变量,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

test.php代码:

<?php 
require 'libs/Smarty.class.php'; //包含Smarty类库文件 
$smarty = new Smarty; //创建一个新的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; //创建一个新的Smarty对象 
$total = 12345; 
$smarty->assign("total",$total); //对模版中的变量赋值 
$smarty->display('test2.htm'); //显示页面 
?>

浏览器显示:

Total is 12345
Formatted Total is 12,345

关于怎么在php中使用smarty修饰变量就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI